6

In my plugin, I want to test to see if jQuery or Prototype (or both) are going to be loaded by another plugin. So, have wp_enqueue_script('jquery') or wp_enqueue_script('prototype') has already been called.

I have code appropriate to my plugin in files plugin.prototype.js and plugin.jquery.js and if Prototype is queued, my plugin will use plugin.prototype.js. This way I avoid loading more than necessary into the site. If neither has loaded, I will queue up whichever is smaller.

How can I test to see what has been queued up? How do I make sure my code runs last?

2 Answers 2

12

Use wp_script_is() to check if a script is in the queue.

function add_my_scripts() {
    $doing_jquery = wp_script_is('jquery', 'queue');
    $doing_prototype = wp_script_is('prototype', 'queue');

    var_dump($doing_jquery, $doing_prototype);
}
add_action('wp_print_scripts', 'add_my_scripts');
5
  • 1
    So does the wp_print_scripts hook get executed right before, during or right after the scripts have printed?
    – artlung
    Aug 12, 2010 at 14:10
  • It prints the scripts, so... during. :) You should use the add_action() priority argument to make your function run later.
    – user66
    Aug 12, 2010 at 14:17
  • Sorry, I misunderstood your question. The wp_print_scripts action runs (allowing you to enqueue your scripts), and the scripts are printed after all the wp_print_script actions are done.
    – user66
    Aug 12, 2010 at 14:19
  • I was unaware of wp_script_is - seems to be exactly what I was looking for. Thanks so much.
    – artlung
    Aug 12, 2010 at 14:24
  • Related to this, can you tell me how to handle plugin script collisions like jQuery Apprise or other popular component? See question here: wordpress.stackexchange.com/q/49020/13
    – Volomike
    Apr 14, 2012 at 16:51
2

To make sure your code runs after jQuery or Prototype is loaded, use the $deps parameter to wp_enqueue_script, and pass either array('jquery') or array('prototype'). If you want to know whether a script is in the queue, you can use the query() method of WP_Dependencies (which is the superclass of WP_Scripts). So something like this should work:

global $wp_scripts;
$jQueryIsLoaded = (bool) $wp_scripts->query('jquery');
$prototypeIsLoaded = (bool) $wp_scripts->query('prototype');

Of course, plugins could enqueue them after you decided on one, so try to make sure you execute this check at the last possible moment.

2
  • How do I execute at the last possible moment?
    – artlung
    Aug 12, 2010 at 14:08
  • Silly me, I didn't see that wp_script_is does this for you, so Adam's answer is the way to go, including the use of the wp_print_scripts hook (which is indeed called just before the scripts are printed, so you can add new scripts there).
    – Jan Fabry
    Aug 12, 2010 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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