1

Lets say the case is "owl carousel".

1- Should you register it as "owlcarousel" or "xyz-owlcarousel" with your theme / plugin prefix ?

2- If 2 or 3 theme / plugins use the same handle "owlcarousel" it loads one right ? If so which one ?

3- From what I see it is common practice to do "xyz-owlcarousel", so how to tackle multiple things loading same script ? Is doing if(jQuery().owlCarousel) check good way to do this ? Is there a better way ?

4- How to avoid the same problem for CSS ?

Thanks in advance

2
  • wp_enqueue_script( string $handle, and the other one for style has the handle that should be unique. This should prevent duplicate loads.
    – prosti
    Jan 5, 2017 at 18:36
  • What if the handles are not unique ? Which one gets loaded ? And if its supposed to be unique, what do you do when xyz plugin loads "xyz-owlcarousel" and abc theme loads "abc-owlcarousel" Jan 5, 2017 at 18:45

1 Answer 1

3
  1. You can use a prefix like 'xyz-' (basically a namespace) for the handle if the script or stylesheet you use is unique to your theme / plugin. Owl Carousel is not, so just 'owlcarousel' is fine. You'd use a prefix if you didn't want any chance another loaded plugin might be using the same name for their script, causing yours not to load.

(As an aside, here is a list of WordPress's default scripts, and the handles used for them, down the page a bit).

  1. Only the first encountered script with a particular handle is used. Any further attempts to enqueue something with the same handle are ignored (reference: notes for wp_enqueue_script). If you needed to specifically use your particular script for a particular handle (e.g. loading a special version of jQuery, and not using WordPress's built-in version), you'd have to deregister the existing one and then register/enqueue yours.

  2. How scripts deal with being loaded multiple times is script-dependent. If a conflict was likely, you could try loading a particular script, such as Owl Carousel, twice (with different handles), and see if it still works okay. Wouldn't it be nice if everyone used a commonly-agreed list of handles for things like Owl Carousel. :)

  3. The same rules/effects apply to CSS stylesheets.

3
  • You are saying i should use "owlcarousel" as prefix when i load owl in my theme / plugin, as should all the other theme / plugins that intend to, but IRL most do NOT. They just use "xyztheme-owlcarousel". Which makes the matters even worse, even if both dont get conflicted, best case scenario the same script gets loaded / tries to get loaded twice. Like you said in 3- In an ideal world, everyone would stick to the standarts we would have OwlCarousel loaded only once. However in that scenario how would we deal with the version changes ? Owl carousel v1 and v2 methods are very different Jan 5, 2017 at 20:44
  • Yeah, just the way it is. If you really need your version of owl carousel, you'd have to unload any existing owl carousel and load yours—assuming they used the same handle, of course, otherwise you'd have to check for owl carousel manually and then handle its deactivation manually too. And it then breaks the other version. This is one of the reasons people have plug-in conflicts. :)
    – iguanarama
    Jan 5, 2017 at 21:54
  • So, all in all, there is no solution :/ Thanks for your comments tho gave a lot to think about Jan 6, 2017 at 0:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.