I have a script that lets me work with multiple shortcodes in WordPress.
function universal_shortcode_handler($atts, $content, $tag) {
// Set up an array of shortcode options
$shortcodes = array(
'ad-code' => 'Advertise with us',
'feedback-code' => 'Share your tips!',
'thanks-code' => 'Thanks for visiting!',
'flagtable' => ''.$flagtable.'',
'home-top' => ''.$home_top.'',
'home-bottom' => ''.$home_bottom.''
);
// Check if the called shortcode exists in our array, and return the appropriate message
if (array_key_exists($tag, $shortcodes)) {
return $shortcodes[$tag];
} else {
return 'This shortcode is not defined.'; // Default message if the shortcode isn't recognized
}
}
// SCRIPTS
// Register each shortcode
add_shortcode('ad-code', 'universal_shortcode_handler');
add_shortcode('feedback-code', 'universal_shortcode_handler');
add_shortcode('thanks-code', 'universal_shortcode_handler');
add_shortcode('flagtable', 'universal_shortcode_handler');
add_shortcode('home-top', 'universal_shortcode_handler');
add_shortcode('home-bottom', 'universal_shortcode_handler');
It’s a huge script, and I would like to reorganize it by including portions of it.
function universal_shortcode_handler($atts, $content, $tag) {
// Set up an array of shortcode options
$shortcodes = array(
'ad-code' => 'Advertise with us',
'feedback-code' => 'Share your tips!',
'thanks-code' => 'Thanks for visiting!',
'flagtable' => ''.$flagtable.'',
'home-top' => ''.$home_top.'',
'home-bottom' => ''.$home_bottom.''
);
// Check if the called shortcode exists in our array, and return the appropriate message
if (array_key_exists($tag, $shortcodes)) {
return $shortcodes[$tag];
} else {
return 'This shortcode is not defined.'; // Default message if the shortcode isn't recognized
}
}
require_once dirname(__FILE__) . '/values.php';
// OTHER SCRIPTS
// Register each shortcode
add_shortcode('ad-code', 'universal_shortcode_handler');
add_shortcode('feedback-code', 'universal_shortcode_handler');
add_shortcode('thanks-code', 'universal_shortcode_handler');
add_shortcode('flagtable', 'universal_shortcode_handler');
add_shortcode('home-top', 'universal_shortcode_handler');
add_shortcode('home-bottom', 'universal_shortcode_handler');
However, when I do that, weird things happen. The script on the include page may stop working entirely, or it may work on some pages but not others.
Is there a way to break this script into bite-size pieces using includes?