Disable plugin CSS and JavaScript

A major strength of Wordpress is the huge number plugins available, often with a simple one click install from the control panel. The number of plugins and the number of developers is unmatched for any other content management system on the internet, however, with this strength comes some weaknesses, for example;

  1. The quality of the code behind each plugin varies wildly, and is often hard to judge without close inspection.
  2. Plugin code is often targeted towards the lowest common denominator to make install and support easier.

One issue I have with a lot of plugins is that they litter the header and footer with css and javascript which may not be required for your particularly use case. Fortunately it is relatively painless to disable CSS and JavaScript embeds using wp_deregister_style and wp_deregister_script.

Let’s take the example of Contact Form 7, to remove the default CSS and JavaScript we would add the following to out /wp-content/themes/YOUR_THEME/functions.php file. The handle should match the wp_enqueue_style or wp_enqueue_script in the plugin file.

add_action('wp_print_styles','remove_styles',100);
add_action('wp_print_scripts','remove_javascript',100);

function remove_styles() {
	// add handles for styles you wish to remove
	wp_deregister_style('contact-form-7');
}

function remove_javascript() {
	// add handles for script you wish to remove
	wp_deregister_script('contact-form-7');
}

blog comments powered by Disqus