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> > <a href="/north-america/" title="North America">North America</a> > <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> > <a href="/north-america/" title="North America">North America</a> > <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.