Home  //  Blog  //  WordPress Optimization  //  How to Deregistering the Default Plugin CSS Files in WordPress

How to Deregistering the Default Plugin CSS Files in WordPress

Posted on August 8, 2010 in WordPress Optimization

With every file loaded into a site an HTTP request is made to the server, and the higher the number of requests made will decrease the overall load time of your site pages and posts. A simple way to cut down on those requests is by combining your CSS and Javascript files into as few as possible.

Disabling the plugin CSS allows you to copy it directly into the main theme style sheet, effectively removing one or more HTTP requests on your site. Although a handful of recent WordPress plugins have begun to offer the option to disable its CSS through its configuration, most plugins still place a call to its CSS file inside the wp_header tag. Disabling the default CSS file requires a simple function, but is dependent on the plugin authors inclusion of the wp_print_styles action.

Search the plugins main php file in the plugin folder for the wp_print_styles item, and it will be closely followed by the script name. The following piece of code uses a sample to disable the default CSS file for the wp-pagenavi plugin:

add_action( 'wp_print_styles', 'my_deregister_styles', 100 );
function my_deregister_styles() {
	wp_deregister_style( 'wp-pagenavi' );
}

Setting Up the Function

Before inserting the code into the functions.php file of the WordPress theme, open your theme header.php and check to see if it contains the following line of code, if not, then add it in the header tag. Note: the wp_header tag is used by plugins to load resources and is required for them to function.

<?php wp_header(); ?>

Navigate into the plugin folder and open the main php file and do a search for wp_enqueue_style. This function will only be possible if it is available in the plugin structure. In the wp-pagenavi.php file, the print scripts action and style name can be found on line 204:

wp_enqueue_style('wp-pagenavi', $css_file, false, '2.70');

In the first argument in the enqueue for the plugin, the name of the style is wp-pagenavi. Copy this name and paste it into the wp_deregister_style line of the full function, as shown in the following example:

wp_deregister_style( 'wp-pagenavi' );

Deregistering Multiple Plugin CSS Files

If you have multiple plugins that use the wp_enqueue_style action, you can add multiple instances of the wp_deregister_style in one single function with the following sample:

add_action( 'wp_print_styles', 'my_deregister_styles', 100 );
function my_deregister_styles() {
	wp_deregister_style( 'wp-pagenavi' );
	wp_deregister_style( 'NextGEN' );
	wp_deregister_style( 'codebox' );
}

Let's Get Social

Use these icons to share this post with friends and colleagues on various social media avenues.

Seems that there are no comments on this post yet. Be the first to have your say!

Submit Your Comment

Please leave a comment by filling out the form below. Comment must be approved before it becomes visible.

Your Comment

Note: You may use these HTML tags and attributes within your comment: <a href="" title="">, <em>, <strong>. To start a new paragraph, make sure you have a full line space between them using two returns.

© Troy Chaplin 2010  //  Site built using a Macbook Pro, Coda and Photoshop and is proudly powered by WordPress