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.

« »