Post Pic

WordPress trick: Disable plugin stylesheet

When activated, some plugins automatically ads their css stylesheet to your WordPress blog. This is great in most cases, but it is a lot cleaner to have all your css styles in one stylesheet. Here’s a hack to disable plugin specific stylesheets.

The first thing to do is to open the plugin file and find the code which include a plugin-specific stylesheet in the blog header. This function is called wp_enqueue_style(). For example, in case of the useful wp-pagenavi plugin, the code to find is:

wp_enqueue_style('wp-pagenavi', get_stylesheet_directory_uri().'/pagenavi-css.css', false, '2.50', 'all');

What we need to find is the handle. The handle is the first argument of the wp_enqueue_style() function, so in the previous example, wp-pagenavi is the handle we need.

Once done, open your functions.php file and paste the following code in it:

add_action( 'wp_print_styles', 'my_deregister_styles', 100 );

function my_deregister_styles() {
	wp_deregister_style( 'wp-pagenavi' );
       // deregister as many stylesheets as you need...
}

Thanks to Justin Tadlock for this great recipe!

4 Responses

Dec 24 2011 17:44

This is a great tip :)

You can also use conditional formatting. eg:

add_action( ‘wp_print_styles’, ‘my_deregister_styles’, 100 );

function my_deregister_styles() {
if ( !is_page(‘Contact’) ) {
wp_deregister_style( ‘contact-form-7′ );
}
}

May 04 2012 02:38

Do you know how to figure out the handle when the plugin was written with CakePHP?

I’ve got this
wp_enqueue_style($this -> pre . ‘style’, $style_url, null, $this -> version, “screen”);

Argh :-(

May 04 2012 02:44

I figured it out – so if anyone else encounters something like this in a plugin…

“pre” is a variable, and luckily it was defined in the same file. the “.’style’” is appending this to the end of the pre variable.

So if pre = myplugin, the handle will be “mypluginstyle”

Hope this helps anyone else that encounters this.

Jul 20 2012 03:01

Nice hack! I’m using this technique to disable unused JavaScript files for a mobile version of a project I’m working on.

So, basically I have a function for detecting mobile: is_mobile() and I have:

if ( ! is_mobile) {
// enqueue scripts needed for desktop experience
} else {
wp_deregister_script(‘foo’);
wp_deregister_script(‘bar’);
}

Seems to work great and should improve the mobile download speed.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required

WP Theme of the week

Sponsored Likebox