• Resolved swani

    (@swani)


    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” 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_actioncall 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)
  • 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 shows how to get the handle for enqueued scripts with code. Some plugins, like Query Monitor can also list the enqueued scripts for you.

    Dion

    (@diondesigns)

    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

    (@swani)

    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 – 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

    (@bcworkz)

    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.