Question

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?

Was it helpful?

Solution

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');

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top