4

I want to include a script in a shortcode output, so I am trying this:

function my_shortcode( $atts, $content = null ) {
    extract(shortcode_atts(array(
            'title'  => '',
           ), $atts));

    wp_enqueue_script('custom-script'); 

}

I have registered the script already and enqueue it if the above shortcode is used. However I want to include the file only once, even if the shortcode is used multiple times. Currently, if the shortcode is used multiple times, it will include the script for each time. Is it possible?

2
  • 4
    will include the script for each time < have you tested this? I don't think enqueue even can produce multiple copies of same script.
    – Rarst
    Nov 17, 2013 at 16:29
  • You're right. I should have tested it more before posting the question. It was problem in my code.
    – enam
    Nov 17, 2013 at 16:41

2 Answers 2

11

The WordPress enqueue system should prevent the inclusion of the same script multiple times, and does when I test it. For example, the following enqueues a Core script and echos a counter. If I put this shortcode into a post 4 times, I get "0123" but the media script only loads one time. The same test works with several other scripts I tried.

function my_shortcode( $atts, $content = null ) {
    extract(shortcode_atts(array(
            'title'  => '',
           ), $atts));
    static $counter = 0;
    echo $counter++;
    wp_enqueue_script('wp-mediaelement'); 

}
add_shortcode('enq','my_shortcode');

If you are getting the same script loaded over and over, there is a problem with your site, but I am not sure where start guessing at what.

5
  • 2
    +1. The underlying premise of the question is wrong, as it misunderstands what it means for WordPress to enqueue a script. Nov 17, 2013 at 19:14
  • it is not prevent duplicate when it is hooked to custom hook.
    – Ari
    Oct 10, 2015 at 23:52
  • I don't know what that means @SoursopTree
    – s_ha_dum
    Oct 10, 2015 at 23:53
  • I tried it using shortcode. I created a custom action hook inside a shortcode function, then hook wp_enqueue_style to it! I checked it using firebug html inspector and I found duplicate stylesheet.
    – Ari
    Oct 10, 2015 at 23:59
  • Sorry, I am wrong! I named the directories and files similarly, so it looks like that there are duplicate.
    – Ari
    Oct 11, 2015 at 0:06
7

You can use wp_script_is() to determine if a script is enqueued - and use it in a conditional, like this:

if( ! wp_script_is( 'custom-script', 'enqueued' ) ) {
    // your code here
}

wp_script_is() takes two parameters - $handle and $list - the latter can be: registered, enqueued/queue, done or to_do; and it returns true or false.

2
  • 9
    While that may not be incorrect, wp_enqueue_script already checks for you, so this code is completely redundant.
    – JakeParis
    Feb 21, 2014 at 13:56
  • You're right about that, nothing more to say, so I generalized the answer. @JakeParis Feb 21, 2014 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.