Remove Plugin Stylesheets and Scripts in WordPress
Many WordPress plugins implicitly inject stylesheets and JavaScript files into the page on each page load. If you don't plan on custom-styling for elements created by the plugin, that's no problem...but you can get caught in a CSS specificity battle if you do intend custom styling. If the plugin is created properly (which is sometimes a big "if" when it comes to WordPress plugins), you can programmatically tell these files not to load from within your given theme.
When scripts and styles are added properly, they use the wp_enqueue_style
and wp_enqueue_script
functions within the plugin files as such:
// Styles Format: wp_enqueue_style($handle, $src, $deps, $ver, $media); wp_register_style('pagination-style', plugins_url('style.css', __FILE__)); wp_enqueue_style('pagination-style'); // Script Format: wp_enqueue_script($handle, $src, $deps, $ver, $in_footer); wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'); wp_enqueue_script('jquery');
Those handle names are incredibly important, as within your theme's functions.php
you'll be adding their counterpart calls of wp_dequeue_style
and wp_dequeue_script
functions:
// Get out of my page! wp_dequeue_style('pagination-style'); wp_dequeue_script('jquery');
In the past I've argued that each plugin should make including their styles and scripts optional, but with these simple functions, I'm not as frustrated as I once was. You'll need to dig into the plugin code to find their script and style handles, but it will be will worth it in the end!
Nice. This is an excellent solution that will update-proof, since the wp_dequeue is in the theme’s functions.php file. Questions: Should this be done for every plugin or should one check to see if they already have a wp_dequeue written in the plugin’s code? And if you perform this across the board will there likely be an error stating the duplicate wp_dequeue? Thanks for another great tip!
Question: If we disable the plugin style by adding a wp_dequeue in the theme’s functions.php, what happens when we (or the client) updates the theme? The theme’s functions.php would be replaced in the process and our wp_dequeue entries would disappear… right?