This quick WordPress tip will show you how to include your scripts and styles only when a specific shortcode is present. Loading scripts on all pages is not recommended because this may cause conflicts and errors on your plugin page where the shortcode has not been active.
WordPress Tip: How to conditionally load your scripts and styles when shortcode is present
You need to replace YOUR_SHORTCODE with your own shortcode to search it in the post, If found you can include the scripts and styles you need to improve the performance of your blog.
add_action('wp_footer','dt_load_conditional_stuff'); function dt_load_conditional_stuff() { global $posts; $pattern = get_shortcode_regex(); preg_match('/'.$pattern.'/s', $posts[0]->post_content, $matches); if (is_array($matches) && $matches[2] == 'YOUR_SHORTCODE') { // load your script wp_enqueue_script('my_script', 'path_to_script.js'); // or stylesheet wp_enqueue_style( 'my_style', 'path_to_css_file.css'); // or include php file include_once 'path_to_php_file.php'; } }
The post Conditionally Adding Scripts and Styles If Shortcode is present appeared first on Deluxe Themes.