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.
2 Answers
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.
-
Thanks, bro. I had forgotten the priority parameter. It works like a charm :)Eh Jewel– Eh Jewel2018-02-24 17:36:13 +00:00Commented Feb 24, 2018 at 17:36
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.
-
your last statement is simply not true. consider using libraries where you don't even have influence over class names.honk31– honk312022-03-22 14:03:37 +00:00Commented 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.Mark Kaplun– Mark Kaplun2022-03-23 17:47:20 +00:00Commented 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– honk312022-03-31 10:09:47 +00:00Commented 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.Mark Kaplun– Mark Kaplun2022-04-01 12:22:13 +00:00Commented Apr 1, 2022 at 12:22
!importantmight be appropriate.