Skip to main content
WordPress made easy with the drag & drop Total WordPress Theme!Learn More

How To Load Scripts In WordPress Only If A Post Has A Shortcode

Last updated on:

As usual it is best practice to load your styles and scripts for your WordPress Themes only when needed, to keep the site loading quickly and to avoid any JS errors. In order to do this, there are tons of built-in conditionals to check if a post is using a specific template, if it’s the homepage, search page, admin page…etc, so you can load your styles and scripts only on the pages that require them, however,  there isn’t any built-in way to test if a post has a shortcode in the content.

Many people simply load all their javascript and CSS for their shortcodes into their header by using the wp_enqueue function, but this is not really the best method. When you are developing a site you should always strive for speed/performance.

Today I got a really handy WordPress function from my buddy at PippinsPlugins (the best site on the web for WordPress plugins at plugin tutorials) which allows you to check if a certain shortcode exits on a post and if it does load your scripts.

Shortcode Check Function

Simply copy and edit (change YOUR_SHORTCODE to the name of the shortcode you want to check for) the function below to test your posts for the existence of a shortcode and load your scripts/styles when needed!

function check_for_shortcode($posts) {
if ( empty($posts) )
return $posts;

// false because we have to search through the posts first
$found = false;

// search through each post
foreach ($posts as $post) {
// check the post content for the short code
if ( stripos($post->post_content, '[YOUR_SHORTCODE') )
// we have found a post with the short code
$found = true;
// stop the search
break;
}

if ($found){
// $url contains the path to your plugin folder
$url = plugin_dir_url( __FILE__ );
wp_enqueue_style( 'my_login_Stylesheet',$url.'plugin_styles.css' );

}
return $posts;
}
// perform the check when the_posts() function is called
add_action('the_posts', 'check_for_shortcode');

Subscribe to the Newsletter

Get our latest news, tutorials, guides, tips & deals delivered to your inbox.

7 Comments

  1. Ryan

    Thanks for this. Does almost exactly what I was trying to do.

    A couple of minor thoughts though.

    I had to switch the stripos line to !== false. It just worked out that the post that had the shortcode I was searching for had it as the only thing in the post so stripos returned 0 which the if missed when 0 was cast as false.

    Also, shouldn’t the break; be within the if? There aren’t any braces on the if so the break is outside and happens the first time it loops.

  2. Joko

    thanks AJ for this article

  3. Antoine Keller

    Since 3.6 it’s even easier with has_shortcode() http://codex.wordpress.org/Function_Reference/has_shortcode

    • AJ Clarke | WPExplorer

      For sure. And actually I was the one that suggested the has_shortcode() function to my friend Pippin who then suggested it for the CODEX 😉

      You can actually check out his initial post on Tuts+

  4. pirolab

    Hi AJ, I’m almost done with my wp plugin but there’s a last thing to do, I’m using

    if( has_shortcode( $post->post_content, 'shortcode') ){wp_enqueue_scrip}

    to check if a page/post content contains the shortcode… that’s ok, but what if I add my shortcode to a custom field (ACF wysing) ,
    $post->post_content won’t recognize the shortcode and my plugin will not work at all

    • AJ Clarke

      Check the custom field as well…

      if ( is_singular() ) {
        if( has_shortcode( get_the_content(), 'shortcode' ) || has_shortcode( get_post_meta( get_the_ID(), 'YOUR_FIELD_ID', true ), 'shortcode' ) ) {
          wp_enqueue_scrip();
        }
      }
      

      I haven’t tested this, but it should work.

Leave a Reply

Your email address will not be published. Required fields are marked *

Learn how your comment data is processed by viewing our privacy policy here.