Combining 2 WordPress Short Codes in 1 Function

Is there a way to combine two different WordPress short codes in one function?

I hired someone to write a script that displays various stuff on the pages of my WordPress site. I’m now fleshing it out, and it looks like this . . .

function geo_symbol_shortcode($atts) {
// Get the current URL
$current_url = $_SERVER["REQUEST_URI"];

// Extract the last segment of the URL
$url_segments = explode('/', rtrim($current_url, '/'));
$last_segment = end($url_segments);

// Next, I have a series of switches that establish various values, such as $ID and $Parent.

// Now, I can display the data . . .
$home_bottom = '';

switch($Parent_URL) {
 case 'usa':
 $home_bottom = '<div class="div--img div--home x--w25"><a href="/usa/" title="United States"><img src="/wp-content/uploads/usa-home.svg" alt="United States Home"></a></div>';
 break;
 default:
 break;
}

return $home_bottom;
}

add_shortcode('home-bottom', 'geo_symbol_shortcode');

It works - but only for [bottom-home]. I also want to put a [top-home] shortcode at the top of the page. However, I apparently can’t handle two shortcodes with one function . . .

switch($Parent_URL) {
 case 'usa':
 $home_top = '<div class="div--bread-crumbs div--world"><a href="/world/" title="World">World</a> &gt; <a href="/north-america/" title="North America">North America</a> &gt; <a href="/usa/" title="United States">U.S.</a></div>';
 $home_bottom = '<div class="div--img div--home x--w25"><a href="/usa/" title="United States"><img src="/wp-content/uploads/usa-home.svg" alt="United States Home"></a></div>';
 break;
 default:
 break;
}

return $home_top;
return $home_bottom;
}

add_shortcode('home-top', 'geo_symbol_shortcode');
add_shortcode('home-bottom', 'geo_symbol_shortcode');

So, I created another function for top-home:

function geosymbol_home_top_shortcode($atts) {

switch($Parent_URL) {
 case 'usa':
 $home_top = '<div class="div--bread-crumbs div--world"><a href="/world/" title="World">World</a> &gt; <a href="/north-america/" title="North America">North America</a> &gt; <a href="/usa/" title="United States">U.S.</a></div>';
 default:
 break;
}

return $home_top;
}

add_shortcode('home-top', 'geosymbol_home_top_shortcode');

I discovered it doesn’t work unless I add the same PHP switches I have in the first function. The problem is there are over 1,000 pages on my site, and there’s probably 2,000 lines of code in my function.

Is there some way to simplify this? Thanks.

Don’t forget you have the atts parameter that is being passed to a shortcode callback as well as the shortcode’s name (lookup add_shortcode() in wordpress docs for more info about these parameters). This will allow you to pass parameters to the shortcode when you use it. This can then be checked in the function to know if it is meant to display the home_top or home_bottom.

As for two different shortcodes, you can create two that point to the same callback. The problem you were having is that you are using two return statements. Keep in mind that once a return statement is encountered, it immediately returns the value and doesn’t process anything after it (thus not going to see the second return statement).

I hope this is making sense. :slight_smile:

1 Like

It’s a little confusing for me, but I’ll play around with it. Thanks.