Menu

On wp_enqueue_scripts and admin_enqueue_scripts

An argument has erupted over the WordPress actions wp_enqueue_scripts and admin_enqueue_scripts vs. init. One of the points was about specificity, and how wp_enqueue_scripts and admin_enqueue_scripts can reduce ambiguity.

I didn’t realize I had strong opinions on it until the issue was pressed, but it turns out I think wp_enqueue_scripts and admin_enqueue_scripts are unnecessary and unfortunate additions to the actions API.

Here’s what I wrote in that discussion thread:

Is Spatula City the store that’s most specifically targeted to the sale of fine spatulas? Yep. But is it the only place you should expect to find spatulas? No. Can you assume that spatulas are not used in a given town if there’s no Spatula City there? No. Would you expect to find grill forks at Spatula City? No. Would it be a waste of time to stop at both the [grocery|hardware|kitchen] store and Spatula City? Yes.

I’m expecting somebody to tell me how I’m wrong, but why not just hook to the init action for all script and style enqueues? It’s certainly less confusing than doing wp_enqueue_style() inside the wp_enqueue_scripts action (even though the codex recommends that semantic ambiguity!). I also suspect it’s more performant to do an if( is_admin() ) test inside an init action than to add_action() on a different action and allow WP to execute the full chain of code necessary for it.