Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 11 down vote accepted

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');
share|improve this answer
1  
So does the wp_print_scripts hook get executed right before, during or right after the scripts have printed? –  artlung Aug 12 '10 at 14:10
    
It prints the scripts, so... during. :) You should use the add_action() priority argument to make your function run later. –  Annika Backstrom Aug 12 '10 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. –  Annika Backstrom Aug 12 '10 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 '10 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 '12 at 16:51
add comment

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.

share|improve this answer
    
How do I execute at the last possible moment? –  artlung Aug 12 '10 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 '10 at 14:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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