2

What priority should I give add_action('wp_enqueue_scripts', 'plugin_scripts');?

Should my plugin scripts and styles load after the theme? Since I don't know what the theme's priority for scripts and styles is, should I arbitrarily select a large number?

In my testing, if I load a stylesheet in both a custom plugin and a custom theme, and don't define a priority for either, the theme seems to take priority and enqueue after the plugin.

  • I'm currently looking to know what best practices there are for themes/plugins to prioritize these things for the Hyperdrive plugin (currently in beta). I would appreciate a ping when you become enlightened. – Josh Habdas May 5 '17 at 10:21
3

Default priorities are fine unless you have special cases where things need to load in a specific order, and even then, you should be defining dependencies for them instead of relying on priority numbers.

Priority numbers are a last resort mechanism, like CSS !important. Avoid setting them unless actually needed.

  • So is my plugin CSS supposed to be overwritten by my theme CSS? – Jacob Raccuia Apr 22 '17 at 15:45
  • I don't understand your question. All CSS that is enqueued gets output into the browser. They don't override each other, you have multiple CSS files. Also, in CSS, "order" doesn't actually matter, only "specificity" matters. – Otto Apr 22 '17 at 19:37
  • if I have body { background:red; } in my plugin css, and body { background:green; } in my theme css and I leave priority blank for both, what color will my site be? – Jacob Raccuia Apr 24 '17 at 13:32
  • Well, first, you should never have that. In CSS, if you want to override one rule with another rule, then the second rule should be more specific than the first. Generally speaking, the one that was loaded last would have precedence, but you should not rely on that behavior. Look up "CSS cascade" to find out more about how CSS works when determining the rules to apply. – Otto Apr 25 '17 at 22:00
  • that does, and well, doesn't answer my question. If the one loaded last has precedence, how can I be sure that the plugin is always loaded second? I don't think I'm going to have the actual problem I laid out above, and all CSS from my theme stylesheet will be specific to the code it builds, but I am confused. One would think plugin CSS would be enqueued after theme css, but apparently that is not the default. – Jacob Raccuia Apr 25 '17 at 23:01

Your Answer

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

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