BlogWeb Design Tricks and Tips

Enqueue the WordPress Stylesheet to Always Take Precedence

By February 15, 2013 February 13th, 2024 No Comments

WordPress aficionados everywhere have declared that enqueueing custom stylesheets and scripts is a best practice, and I am not one to go against the word of the experts, thus that is how it should be done.

I kept finding however that once I would enqueue the stylesheet, I would still have times when the theme’s primary stylesheet would take precedence.  So I dug deeper and found that I also needed to set priority on my stylesheet because I wanted my custom styles to have the final say.  So this is how I finally got all of my custom styles to take precedence.  In this example, the stylesheet is titled “custom.css” and I set the priority at 12 to make sure that it was the boss…

Hope it helps and works for you!

 

Enqueueing the WordPress Stylesheet and Setting Priority:

 

/*————————————————————————————————*/
/*———————- Adding Custom Stylesheet————————————–*/

/*———————————————————————————————–*/

function add_sweet_p_css() {

// Register stylesheet

wp_register_style( ‘sweet-p-style’, get_template_directory_uri() . ‘/custom.css’ );

// Enqueue stylesheet

wp_enqueue_style( ‘sweet-p-style’ );

}

add_action( ‘wp_enqueue_scripts’, ‘add_sweet_p_css’, 12 );