This is a followup to my previous post on adding scripts only to the WordPress pages that need them. Please read that first, or this might not make much sense…
To cleanup the dynamically generated head section even more, you can disable the styles associated with scripts that aren’t loaded on every page.
The easy way to do this is very similar to what I talked about last time – add this code to your functions.php file (or custom-functions.php if your theme supports it):
add_action( 'wp_print_styles', 'my_deregister_styles', 100 );
function my_deregister_styles() {
if ( !is_page('about-us') ) {
wp_deregister_style( 'thickbox' );
wp_deregister_style( 'style' );
}
}
The plugin I’m working on is WPNG Calendar, which displays a stream of events from Google Calendar. I want to show it, in this case, only on the site’s About Us page. I’ve already deregistered the scripts associated with WPNG Calendar on most pages, so now I want to hide the styles on the same pages (why load them if you don’t need them?).
So now you’ll do the same thing as in the previous post with a twist:
- View the source for a page on your site and find the CSS stylesheet links associated with the plugin that you’ve already deregistered on most pages;
- Open up the file that calls that stylesheet – in my case I’m looking at wpng-calendar.php.
- Search for wp_enqueue_style within the PHP file and find the handle for the stylesheet you want to deregister. The handle is the first element in parentheses after the wp_enqueue_style element – in the snippet above they are thickbox and style.
A Problem That You Can Fix Yourself!
What happens if you can’t find a ‘wp_enqueue_style’ in your PHP file? This is unfortunate; it means that the plugin creator isn’t making easy for you. Using wp_enqueue_script and wp_enqueue_style makes it simple to work with the tools WordPress has built-in, but there’s a way around this.
Look for a link that calls a stylesheet – something like this:
echo '<link type="text/css" rel="stylesheet" href="' . get_bloginfo('wpurl') . '/wp-content/plugins/wpng-calendar/css/style.css" />';
Comment that line out and add your own wp_enqueue_style line, replacing the handle with the stylesheet name and editing the source link as needed (refer to this reference for parameters, I didn’t include what I wasn’t sure about):
wp_enqueue_style('style', get_bloginfo('wpurl') . '/wp-content/plugins/wpng-calendar/css/style.css', false, '', 'screen');
Now you have a stylesheet being loaded in the way WP intended it to happen and you have a handle (‘style’) that you can plug into the deregister function.
Do the same thing for any other stylesheets being called, add those handles to the function and you should be good to go. This is the final version of my function:
add_action( 'wp_print_styles', 'my_deregister_styles', 100 );
function my_deregister_styles() {
if ( !is_page(array(2,'events','about-us')) ) {
wp_deregister_style( 'thickbox' );
wp_deregister_style( 'style' );
}
}
You can see that I’m hiding the styles from the head section on all but three pages:
if ( !is_page(array(2,'events','about-us')) ) {
I hope this is helpful – please let me know if you find these two posts on restricting plugin loading useful and understandable.
« WordPress – only load plugins on pages that need them Portfolio additions – Loveland Habitat and more »


Yes! Thanks for the tip, I have a file I include from the functions.php in your theme directory. I call it ‘init.php’ Previously I was removing them at the ‘head’ action but doing it at ‘wp_print_styles’ is perfect. I now have 3 deregister funcitons, it’s how I keep my site minified you can check it out here: http://www.focusem.com
Check the YSlow rating, if I can get the client to sign up for SimpleCDN I’d have an A rating, it’s not far from perfect!
Very nice. I need to work on learning how to minify my own WP sites.
In your first code example, shown in green, the word ‘thickbox’ is missing an apostrophe at the beginning, which will break the code if people try to copy & paste. Thanks for the post, great help!
Hey, thank you for pointing that out – I’ve made the edit to the code snippet.
I love your site’s design. But your coding examples using green text is impossible to read!
Keep up the great articles
Thanks for mentioning it – it’s something I’ve meant to do but it keeps floating to the back of the list. I’ll make a note to change the color this week.
This should be much easier to read – for some reason it was converting code to span tags. No idea…
Cool. Now what’s the “100″ for?
It’s a priority, I think 10 is the default, and anything more than 10 would load later. 100 is a pretty low priority but that’s what’s suggested in the articles I read about this function.