bnks.xyz

Menu

Conditionally loading plugin scripts in WordPress

I’m a big fan of WordPress – it’s both easy to use as an author and easy to develop for. There are also a huge amount of free (and paid) plugins to extend the core functionality.
Unfortunately there is a downside – most plugins do not conditionally load their files only on pages that call them. It is all to easy to end up with every page loading a string of Javascript and CSS files slowing the site to a crawl, when they are only actually required on specific pages.

I’m going to show 2 quick examples of how to use the functions.php file in your theme to only load the scripts for a plugin on a specific page using deregister and remove_filter, which are built into WordPress.

Example 1

Let’s start with Contact Form 7 – this is a great plugin to give very customisable and extendable contact forms.
In functions.php, just add:

// Remove Contact Form 7 CSS + JS form all pages except Contact
add_action( 'wp_print_scripts', 'deregister_cf7_javascript', 15 );
function deregister_cf7_javascript() {
    if ( !is_page(15) ) {
        wp_deregister_script( 'contact-form-7' );
    }
}
add_action( 'wp_print_styles', 'deregister_cf7_styles', 15 );
function deregister_cf7_styles() {
    if ( !is_page(15) ) {
        wp_deregister_style( 'contact-form-7' );
    }
}

This will first prevent the Javascript, and then the CSS file from being loaded unless the Page ID is 15 (obviously change this to the ID of your Contact page or wherever you use the script – to get this open the page for editing in the dashboard and look at the URL).
The names deregister_cf7_javascript and deregister_cf7_styles – both mentioned twice) are arbitrary, just make sure they are unique. However contact-form-7 is set by the plugin and may be different between the Javascript and CSS files.
However this will still be calling an extra CSS file when really you could (should) just include the code in your main style.css. So take it a step further and remove the if statement from the 2nd part to never load the Contact Form 7 stylesheet and always use your own:

add_action( 'wp_print_styles', 'deregister_cf7_styles', 15 );
function deregister_cf7_styles() {
    wp_deregister_style( 'contact-form-7' );
}

Example 2

Next let’s look at Slickr Flickr – another popular plugin for displaying Flickr galleries on your site.
This is slightly more complicated as this plugin uses a filter to load it’s initialising script and also exactly which scripts it tries to load will vary on your settings. This example is based on having selected Load Scripts in the Footer and use Thickbox as the Lightbox as this is quite a common use.

// Remove Sickr Flickr JS files from all pages except Gallery + CSS from all pages.
add_action( 'wp_print_scripts', 'deregister_slickrflickr_javascript', 17 );
function deregister_slickrflickr_javascript() {
    if ( !is_page(17) ) {
        wp_deregister_script( 'slickr-flickr' );
    }
}
add_action( 'wp_print_scripts', 'deregister_slickrflickrstart_javascript', 17 );
function deregister_slickrflickrstart_javascript() {
    if ( !is_page(17) ) {
        remove_filter( 'print_footer_scripts', 'slickr_flickr_start',100 );
    }
}
add_action( 'wp_print_scripts', 'deregister_thickbox_javascript', 17 );
function deregister_thickbox_javascript() {
    if ( !is_page(17) ) {
        wp_deregister_script( 'thickbox' );
    }
}
add_action( 'wp_print_styles', 'deregister_slickrflickr_styles', 17 );
function deregister_slickrflickr_styles() {
        wp_deregister_style( 'slickr-flickr' );
}

As before, 17 should be replaced with the ID of the page where you are displaying your gallery. One important thing here is that the remove_filter statement has 3 variables specified when only the first 2 are compulsory. This is because the plugin is specifying a non-default priority (100 and so you must specify it here also otherwise it won’t work.

Wrap up

With deregister and remove_filter in your armoury you can significantly speed up your site making for a much better user experience.
If you want to take it further – you could use this as the basis for concatenating your scripts into a single .js file (or 1 per library if you are using multiple ones) – just remove the conditional statements to always prevent loading. Then register and load your own file in their place. Just remember to keep it up to date as the plugins get updated!
Don’t forget, HTTP requests are expensive – always bundle your CSS together and if possible your Javascript too.

One caveat with this is that not all plugins are coded according to the guidelines – they may not register their scripts making this potentially useless. However you should be OK with the popular ones – even if you have to look in the code to see whether you need to use deregister or remove_filter.

5 thoughts on “Conditionally loading plugin scripts in WordPress

Leave a Reply

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