Can wp_script_is used in pluginA check if a script is being enqueued/registered from pluginB?

Can wp_script_is used in pluginA check if a script is being enqueued/registered from pluginB?

I have two plugins both of which use Angular. One plugin (pluginA) has angular.min.js being loaded across all pages. The other (pluginB) has angular.min.js being loaded on only one. I would like to check if pluginA has already enqueued Angular but I want to do it from pluginB.

For instance, I tried

$handle="myAppScript.min.js" // this is a concatenated/minified version of the entire pluginA angular app.
$list="enqueued" // I also tried all the other $list options.
// if 'myAppScript.min.js' is present, don't register Angular again, otherwise register it.
if (wp_script_is($handle, $list)) {
  return; 
} else {
  wp_register_script( $this->plugin_name . '-ajs', plugin_dir_url(__FILE__) . 'js/angular/angular.min.js', array(), $this->version, false );
}

However wp_script_is returns false no matter what I try (I double checked with a var_dump) and therefore adds Angular again.

Thanks for any help you can give!

Update – I have a working solution to my particular problem using is_plugin_active, but I’m still curious if there’s an answer to the question. Thanks!

    $plugin = 'pluginA/pluginA.php';
    include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
    if (!is_plugin_active($plugin)) {
      wp_register_script( $this->plugin_name . '-ajs', plugin_dir_url(__FILE__) . 'js/angular/angular.min.js', array(), $this->version, false );
    }

3 Answers
3

Now that I see you were hooking into plugins_loaded, this makes sense because it is fired much earlier than wp_enqueue_scripts. It is expected that your function would return 0 at this point.

See here and here for the order in which actions are generally fired.

Try to add an action at wp_enqueue_scripts with a late priority to make sure it runs after other enqueued scripts. Then you can deregister the script and register again with new parameters.

If you try to register or enqueue an already registered handle with
different parameters, the new parameters will be ignored. Instead,
use wp_deregister_script() and register the script again with the new
parameters.

Edit: Sorry, I misread that. is_plugin_active is not an action hook and will immediately return true without telling you whether the script is enqueued. It still stands that you must hook the function wp_script_is at wp_enqueue_scripts or later.

Leave a Comment