1

If I enqueue styles from my plugin, then it loads after theme's styles. That's why some CSS of my plugin is overriding by theme's CSS. This problem would be fixed if I can ensure my plugin's styles load after theme's styles.

1
  • Theme styles load last because that's the presentation layer chosen by the user and should take precedence over plugin styles. If your plugin's styles are important for functionality, then they should be namespaced so that your classes and IDs don't conflict with the theme's. You should also use specificity as a tool for influencing how styles are applied. If something is really important then !important might be appropriate. Feb 24, 2018 at 8:57

2 Answers 2

2

It's easy to add your plugin stylesheet after theme stylesheet. If you're sure that your theme styles and plugin styles have the same selector weight (theme has this style .site-header { background-color: #ccc; } and your plugin has this .site-header { background-color: #f1f1f1; }) then enqueuing plugin stylesheet after theme stylesheet will work.

If you're enqueuing with wp_enqueue_scripts action hook then changing the hook priority parameter will do the work. Here's an example:

function op_enqueue_scripts() {
    wp_enqueue_style( 'bootstrap', 'https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css' );
}
add_action( 'wp_enqueue_scripts', 'op_enqueue_scripts', 50 );

If priority 50 doesn't work then try increasing that to 80 or 100.

1
  • Thanks, bro. I had forgotten the priority parameter. It works like a charm :)
    – Eh Jewel
    Feb 24, 2018 at 17:36
0

No. (or technically there might be, but in a few days there will be a question here about how to enqueue theme styles before plugin styles).

If your styles are getting overridden by theme styles it means that you do not properly prefix your classes.

4
  • your last statement is simply not true. consider using libraries where you don't even have influence over class names.
    – honk31
    Mar 22, 2022 at 14:03
  • didn't get what you are trying to say. do you use two libraries that don't prefix their CSS? well, sucks to be a user of such low quality libraries. Mar 23, 2022 at 17:47
  • no. all i'm saying is, that when you use libraries, you don't have control over class names. no chance for "proper prefixing" as you put it..
    – honk31
    Mar 31, 2022 at 10:09
  • if libraries do not prefix their css rules, you probably should not use them. So you put more priority for the plugin rules, what happend to the theme relates rule? you just going to ignore all of them? This is a dangerous game to play. Apr 1, 2022 at 12:22

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.