Home > WordPress Tips and Tricks > Excluding Plugin Related File on Certain WordPress Page

Excluding Plugin Related File on Certain WordPress Page

September 18, 2013

By adding code snippets to your themes functions.php file you can remove the javascript and CSS from all pages that don’t use that particular wordpress plugin which makes your site load faster.

Here is an example for the Contact 7 form wordpress plugin. This snippet removes the JS files.

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

Remove Contact 7 css files

add_action( 'wp_print_styles', 'deregister_cf7_styles', 100 );
function deregister_cf7_styles() {
    if ( !is_page(100) ) {
        wp_deregister_style( 'contact-form-7' );
    }
}

In both code snippets, change the number 100 to the id of the page where you have added the contact form or use the page slug instead. Note you can change the !is_page  to the inverse of  is_page and then this would remove the files only from that particular page.

Reference for this article.

WordPress Tips and Tricks

  1. Cynde Delaina
    January 10, 2014 11:22 pm | #1

    If you could email me with a few ideas on how you made your blog look this excellent, I would be thankful.

  1. No trackbacks yet.