Scenario: It is common that a JS dependency is bundled with a style file to work properly (just think about your favorite slideshow jQuery plugin). AFAIK, in this situation, the script and style have to be included in separate wp_enqueue_script and wp_enqueue_style calls.

I am thinking about how to automate this process.

A solution I could come about is to declare the configuration in a .json file in which an entry looks like this:

"jquery-plugin": ["static/js/jquery-plugin.js", "1.0", ["jquery"], "jquery-plugin.css"]

(the configuration is [file_path, version, js dependency, bundled css file]).

When my plugin is loaded, the .json file is read, parsed and then the scripts and styles are registered. Also, information about whether a script has a bundled style file is kept in a global array, let's call it $_scirpts_has_style. With $_scirpts_has_style, the bundled style can get wp_enqueue_style'd automatically after the corresponding script is wp_enqueue_script'ed.

So, what I basically want is some signal which informs me that a script is enqueued, but I cannot find such signal (action/filter). script_loader_src is something very close but it is triggered when the script is getting printed, which is too late to enqueue a style file.

Is there any action/filter that does this? Or,

What's your solution to this problem?

  • 1
    There must be a specific condition to trigger the enqueuing of your script. Why don’t you use the same condition for your stylesheet? – fuxia Apr 16 '13 at 5:07
  • @toscho You're right. But consider this: Once the user enqueues a script, all its dependencies are enqueued as well, which is done automatically and silently by WP. Auto-dependency resolving is a certain benefit to rely on, otherwise there's no meaning to use wp_enqueue_script system. – kavinyao Apr 16 '13 at 9:48
  • This is a very good question that I wondering about myself, what is the point of enqueuing script dependencies automatically if they can't be coupled with their dependent styles? Its a big design hole imo. Has anyone had any luck solving it? – h.coates Aug 2 '14 at 6:49
  • I think this is an excellent question, but for now, the only thing you could do is develop your own function which does this. WordPress is old and the purpose, in the beginning, was just blogging. I hope they will do something about there dependency issues. – Remzi Cavdar Dec 8 '18 at 8:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.