Home / functions.php / Enable Contact form 7 on specified pages only otherwise wp_deregister_script
  • Enable Contact form 7 on specified pages only otherwise wp_deregister_script


    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.

    wordpress snippet : PHP

    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' );

    ( WordPress codex functions, hooks, in this snippet. )

    register, is_page, add_action, wp_deregister_script, wp_deregister_style, wp,
  • 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!

Build a better with wpsnipp.com

Weekly snippets, themes, deals coupons roundup.

x No Thanks