How to load script-related styles automatically?

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.

Read More

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?

Related posts

Leave a Reply