Title: Duplicate script in my plugin
Last modified: April 9, 2018

---

# Duplicate script in my plugin

 *  Resolved [swani](https://wordpress.org/support/users/swani/)
 * (@swani)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/duplicate-script-in-my-plugin/)
 * Hello,
 * I created a plugin for a client that creates an image gallery shortcode. The 
   shortcode uses a few resources that are already enqueued by another plugin. I
   don’t want to remove my code and assume that the resource will be there because
   the client could disable the other plugin and then mine would break. So I Googled
   around and found “[wp_script_is](https://codex.wordpress.org/Function_Reference/wp_script_is)”
   which seems perfect. The codex even says:
 * > Determine if a script has been registered, enqueued, printed, or is waiting
   > to be printed. Very useful when registering/enqueing scripts in plugins to 
   > avoid conflicts with other plugin scripts.
 * There are four possible values listed for `$handle`: `registered`, `enqueued`,`
   to_do` and `done`. I have tried them all using a copy/paste from the codex.
 *     ```
       $handle = 'jquery.swipebox.min.js';
       //$list = 'registered';
       $list = 'enqueued';
       // $list = 'to_do';
       // $list = 'done';
       if (wp_script_is( $handle, $list )) {
         return;
       } else {
         wp_enqueue_script( 'se2-swipebox-js', $path_to_plugin_folder . 'js/jquery.swipebox.min.js', array( 'jquery' ), '1.0.0', true );
       }
       ```
   
 * Regardless of which value I use for handle, the script is printed. I have tried
   to adjust the priority of my `add_action`call but that didn’t help either. Anyone
   see what I’m not doing right here?
 * Thanks,
    Swani

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [Jacob Peattie](https://wordpress.org/support/users/jakept/)
 * (@jakept)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/duplicate-script-in-my-plugin/#post-10161856)
 * The handle of the script isn’t the filename, it’s the first argument of `wp_enqueue_script()`.
   For your version the handle is `se2-swipebox-js`.
 * So if you want to check if the other plugin’s version of the script is loaded
   you need to find out what handle they gave it and use that in your call to `wp_script_is()`.
 * [This Stack Exchange answer](https://wordpress.stackexchange.com/questions/54064/how-do-i-get-the-handle-for-all-enqueued-scripts)
   shows how to get the handle for enqueued scripts with code. Some plugins, like
   [Query Monitor](https://wordpress.org/plugins/query-monitor/) can also list the
   enqueued scripts for you.
 *  [Dion](https://wordpress.org/support/users/diondesigns/)
 * (@diondesigns)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/duplicate-script-in-my-plugin/#post-10162025)
 * Check the other plugin’s code to see when/where it enqueues the `se2-swipebox-
   js` script. Then make sure you enqueue your version of the script before that.
 * But…if your script uses the same handle as the other script, and it’s the same
   code, then you don’t need to check whether the other script has been enqueued.
   WordPress will ignore all but the first call to enqueue the script.
 *  Thread Starter [swani](https://wordpress.org/support/users/swani/)
 * (@swani)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/duplicate-script-in-my-plugin/#post-10162098)
 * OK, I’m feeling a little silly. I just ignored the “handle” part of this completely.
   I switched the `if` statement to use the handle and it worked immediately. Did
   the same for the CSS with `wp_style_is` for anyone who has stumbled upon this
   thread for the same issue.
 * So, thank you both for your help. I do have one follow up question for either
   of you or anyone else. Let’s say I’m coding this for the wider world, rather 
   than just this client. The resource is Swipebox in this case, but it could have
   been anything. Since a handle is set by the developer, what is best practice 
   for avoiding this situation in general? I think part of the reason I made the
   mistake I did is that it seemed intuitive to me that using the file name would
   be the best way to find overlapping resources, since the most common type of 
   overlap would be a file that is taken from elsewhere, rather than coded yourself.
   Also, the Codex says:
 * > This would check if the script named ‘fluidVids.js’ is enqueued. If it is enqueued,
   > it does nothing. If it is not enqueued, the files are then registered and enqueued.
 * Since this says “named” I thought I needed to use the file name, rather than 
   the handle. And, as I said, that made the most sense to me as to how this function
   would work.
 * [@jakept](https://wordpress.org/support/users/jakept/) – You provided a link 
   to a StackExchange answer about how to get all the handles, but even if I did
   that, I wouldn’t really know what to check the list for. Does that make sense?
 * Swani
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/duplicate-script-in-my-plugin/#post-10162860)
 * Regarding lists of handles, you are right, a list of just the handles is not 
   very informative. The SE suggestions for code are not all that helpful IMO. I
   suggest you var_dump() the entire global $wp_scripts. The output will then include
   external file references for each handle. You presumably know what files you 
   need to enqueue. If they are listed in $wp_scripts, they are already registered
   and you merely need to enqueue the associated handle or include it in a dependency
   array when enqueuing related scripts.
 * Which leads us to your other question about how to determine if the script of
   another resource is registered. Everything there is to know about registered 
   scripts is contained within global $wp_scripts. Your code just needs to check
   if the desired script is listed. You must first allow other resources time to
   register their scripts before you can check for their presence. Do so by hooking“
   wp_enqueue_scripts” with a very large priority number to ensure your callback
   runs after all the other added hooks.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Duplicate script in my plugin’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 4 replies
 * 4 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [8 years, 2 months ago](https://wordpress.org/support/topic/duplicate-script-in-my-plugin/#post-10162860)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
