Remove Stylesheets That Has Been Enqueued
Wordpress will create a queue of stylesheets to load and will import these when it runs the wp_head function. The benefit of this system is that you can call the wp_enqueue_style function from any plugin and it will be placed at the top of the where it's meant to be. But this can also cause a problem, for example what if you have installed a plugin that uses the Modernizer CSS library to correct some cross browser problems, but this plugin hasn't been updated for a while and is using an old version of Modernizer. Then you have another plugin this also uses the Modernizer CSS library, but this plugin is newer and has the latest version of Modernizer. You don't want to load both versions, you will only want the latest version...so what can you do?
You can open the plugin and change the code to remove the line where it adds the stylesheet. The problem with this is that if the plugin does get updated it will override your change and the modernizer file will be added again. When a plugin is updated it will replace the entire file so any changes you make will be gone.
The other solution is to use the wp_dequeue_style() function in your theme functions.php to remove the stylesheet from the enqueue.
wp_dequeue_style( $handle );
This will now stop the version 2 from loading which allows you to freely load the latest version on your webpage.
Placing this code in your themes functions.php file will mean that if the plugin does get updated then you will still be removing the modernizer version 2.
wp_dequeue_style( 'jquery' );
Due to the fact that some developers might actually have to do this to remove existing stylesheets from plugins brings up a couple of questions.
- Should we still be using outdated, unsupported plugins?
Learn how to code with Treehouse
- Learn projects with access to 1000+ videos
- Practice live with our Code Challenge Engine
- Get help in our members-only forums
Join the weekly newsletter
Sign up for our newsletter. You'll be updated on new tutorials as they come available.
Subscribe today and get a 50% discount code to Paulund