2

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
  • When are you calling the function wp_script_is? If it is called before the wp_enqueue_scripts action is fired, then it should return 0. codex.wordpress.org/Plugin_API/…
    – iyrin
    Mar 29 '17 at 18:18
  • Thanks for the idea. I'll have to recreate the situation. Earlier I pushed the code using is_plugin_active.
    – aberkow
    Mar 30 '17 at 16:18
  • Ok that is definitely before scripts are enqueued. Take a look here. codex.wordpress.org/Plugin_API/…
    – iyrin
    Mar 30 '17 at 16:25
1

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.

0

You are on the right path. There is no better way to determine script is enqueued or not. This is efficient way wp_script_is( $handle, $list )

Just wondering if your $handle name is correct. Make sure $handle while checking wp_script_is is the same as when you enqueue script in pluginB. The first parameter is $handle. Refer to this WP Codex.

It says:

Parameters

$handle

(string) (Required) Name of the script. Should be unique.

I believe you are using plugin name as a handle in else part.

Try using same name "AngularJS" or something but must be the same as $handle

0

Maybe I'm wrong but according to WP codex:

wp_script_is( string $handle, string $list = 'enqueued' )

$handle - unique name of the script

As far as I can see you are using FILE NAME instead which is wrong. By me it has nothing to do with when actions/hooks are being fired coz both of your plugins are fired on plugins_loaded.

So, if you check it out this way

$handle = 'MY_SCRIPT_NAME'; /* can't really be $this->plugin_name . '-ajs' because it gives 2 different strings */

in both of your plugins, only one instance of required JS file will be loaded.

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.