wordpress snippets last updated   : August 18, 2014
we now have 638 snippets

Enable Contact form 7 on specified pages only otherwise wp_deregister_script

wordpress snippet

Like many plugins Contact form 7 adds CSS and Javascript to the head of your HTML document. However like many plugins this is done on every single page regardless if the code is required. Adding this snippet to the functions.php of your wordpress theme will let you enable Contact form 7 only on specified pages.

You will see !is_page(array(8,10)) within each array, the 8 and 10 are the IDs of the posts you wish to add the CSS and Javascript to head of your HTML document.
snippet :  PHPcopy
add_action( 'wp_print_scripts', 'deregister_cf7_javascript', 100 );
function deregister_cf7_javascript() {
    if ( !is_page(array(8,10)) ) {
        wp_deregister_script( 'contact-form-7' );
    }
}
add_action( 'wp_print_styles', 'deregister_cf7_styles', 100 );
function deregister_cf7_styles() {
    if ( !is_page(array(8,10)) ) {
        wp_deregister_style( 'contact-form-7' );
    }
}

Did you like this post? If so please share it with your friends. Thanks!

  • http://www.facebook.com/smbm.tom Tom Lambie

    For a few years we were using this plugin on clients sites and this was very annoying. In fact it’s the main reason we stopped using it. Anyway, glad there’s now a ‘fix’ for this issue, I’m sure many people will be thankful for it

    • http://wpsnipp.com Kevin Chard

      No problem glad to help,

  • http://twitter.com/PrinceDotGr P. Velisarakos

    Don’t forget to issue wp_dequeue_style( ‘contact-form-7′ ) and wp_dequeue_script( ‘contact-form-7′ ) after wp_deregister_style( ‘contact-form-7′ ) and wp_deregister_script( ‘contact-form-7′ ) respectively!

jqsnipp.com | we just launched jquery snippets website