WordPress – only load plugins on pages that need them

The WordPress site I’m working on now (WP 2.9.1) has gotten very slow – in part I’m sure that’s because of the large number of plugins that are now loading every single time. I looked around for a bit to find some resources online for restricting plugin loading by page, so I’ll summarize what I did here (not being a PHP coder I didn’t understand at first how to accomplish it, but this is the less-techie version).

This is the snippet of code that got me started, from Justin Tadlock’s blog post; it will disable Contact Form 7’s loading site-wide:

add_action( 'wp_print_scripts', 'my_deregister_javascript', 100 );
function my_deregister_javascript() {
wp_deregister_script( 'contact-form-7' );
}

This snippet goes in the functions.php file (or custom-functions.php if your theme uses that).

But if you want Contact Form 7 to show up only on your contact page, how do you allow it to do that? By using an if statement, like one of the commentors on Justin’s post suggested:

add_action( 'wp_print_scripts', 'my_deregister_javascript', 100 );
function my_deregister_javascript() {
if ( !is_page('Contact') ) {
wp_deregister_script( 'contact-form-7' );
}
}

This snippet will disable Contact Form 7’s loading on all pages except Contact. So now, how to use this for other scripts?

Disabling Scripts on a Page-by-Page Basis

First thing you’ll need to do is find out what’s loading that shouldn’t be. View the source code for one of your site’s pages and make a list of which plugins are loading that aren’t needed site-wide. For example, in my site I’m using WPNG Calendar plugin on my Events page but nowhere else, so that’s one I need to restrict.

Next, you need to find the handle for each script. To do this, you’ll need to open up the main PHP file for each plugin that’s on your list. I did this for WPNG Calendar by going into the plugins folder in FTP and opening wpng-calendar.php in an editor, then doing a search for wp_enqueue_script, which loads JavaScripts into generated pages in WordPress. The handle is the first element in parentheses in each wp_enqueue_script line.

wp_enqueue_script(‘wpng-calendar‘, get_bloginfo(‘wpurl’) . ‘/wp-content/plugins/wpng-calendar/js/functions.js’, array(‘date-js’), ‘0.85’);

In this case the handle is ‘wpng-calendar’ but I found that there were a number of other scripts being called into play by wp_enqueue_script – date-js, jquery-js, thickbox-js and wiky-js. I’ll also add those to my deregister function so that they’re only called on the Events page.

Potential Problems

As Justin Tadlock stated – not all plugins use the wp_enqueue method for calling scripts (and styles too) because they’re not aware of it. Using wp_enqueue makes it really easy to use WordPress’ built-in tools for managing scripts and styles. If you can’t find that in the particular plugin you want to hide, I’m not sure how you should proceed yet – I’ve encountered the same problem and don’t have a fix now.

Forward…

This is the snippet I added to functions.php to restrict loading of the calendar on all pages except Events:

add_action( 'wp_print_scripts', 'my_deregister_javascript', 100 );
if ( !is_page('Events') ) {
wp_deregister_script( 'wpng-calendar' );
wp_deregister_script( 'date-js' );
wp_deregister_script( 'thickbox-js' );
wp_deregister_script( 'jquery-js' );
wp_deregister_script( 'wiky-js' );
}
}

This worked perfectly. And you can do the same with any other plugin scripts that aren’t needed site-wide. If you need to show the plugin on multiple pages, change the if line to this:

if ( !is_page(array(2,'events','about-us')) ) {

Check out the conditional tags page in the WordPress Codex for more information on selecting multiple pages.

22 comments

  1. Debbie, thanks so much for posting this! I’m writing a plugin of my own and want it to be as efficient as possible, so I’m using what you’ve written here (and in the follow-up post) to keep it from cluttering up unrelated pages.

    P.S. Good luck on Idea X!

  2. That’s a great idea! Just what I need, but I’m trying to do the same thing in a category page (E.g.: load the plugin X only whitin the category-y page) with not sucess. :/ Maybe somebody can give me an idea of how to do it. :)

  3. Shouldn’t it be something like:

    add_action( ‘wp_print_scripts’, ‘my_deregister_javascript’, 100 );
    function my_deregister_javascript() {
    if ( !is_category(‘mycategory’) ) {
    wp_deregister_script( ‘myplugin’ );
    }
    }

  4. Yes – you’d use the second code snippet in this post, changing ‘Contact’ to the name of your about us page, and changing ‘contact-form-7′ to the name of your JS file.

  5. This is so very awesome. Now that I have 24 plug-ins running at the same time, my header is clogged with css and js.

    As to XGP’s problem, wanting to only load a single JS file on a single page, that seems better handled with a conditional in the header.php file.

    Thanks for posting this Debbie. I’ll give it a try!

  6. This shows how to disable the plugins on all but some selected pages. What I would like to know, is how to disable the plugins just on some pages. This could really help for a site with a lot of pages but a few that don’t need the plugins…

  7. Does this method still work with wordpress 3.7 / 3.8, I’ve been trying this but to no effect :(

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">