How To Load the Contact Form 7 Script for a Contact Page Only

  • Buffer
  • Sharebar
  • Buffer

You can stop the Contact Form 7 plugin’s script and stylesheet from loading on every page of your WordPress blog. While the plugin is a great one to install, since it makes creating contact forms easy, you probably don’t want to load the script and stylesheet on every page. For those that one to get every ounce of performance out of their blog, you definitely don’t want those two resources to load each time.

The good news is that it is easy to only load the plugin’s resources on a specific page of your WordPress blog. By simply creating a few lines of code in the functions.php file of your current theme, you can control when scripts and stylesheets are loaded for many plugins that you have installed. Let’s look at how to do this for the Contact Form 7 plugin.

Deregistering Scripts and Stylesheets

wordpress logo 300x300 How To Load the Contact Form 7 Script for a Contact Page Only

Many plugins use the wp_enqueue_script WordPress function to load supporting Javascript files. The wp_enqueue_style function may be used to load any stylesheets used by the plugin. Now, not all plugins use these two functions as they may use PHP’s “echo” command instead. If a plugin does use the WordPress functions, you can then use two other WordPress functions to unregister the resources when they aren’t needed. The two WordPress functions are: wp_deregister_script and wp_deregister_style.

To unregister either a stylesheet or script you will need their handle, which was created when the wp_enqueue_script or wp_enqueue_style was called. You can easily find this by searching through the plugin’s files until you find where the stylesheet or script is registered.

For the Contact Form 7 plugin, the following lines of code are used to register the script and stylesheet:

wp_enqueue_script( 'contact-form-7', wpcf7_plugin_url( 'scripts.js' ), array( 'jquery', 'jquery-form' ), WPCF7_VERSION, $in_footer );
wp_enqueue_style( 'contact-form-7', wpcf7_plugin_url( 'styles.css' ), array(), WPCF7_VERSION, 'all' );

The code highlighted in green shows the two handles that will be used to deregister the resources. Next we must add the code needed to prevent the script and stylesheet from loading for each page.

Deregister the Contact Form 7 Script and Stylesheet

To only load the Contact Form 7 Javascript and stylesheet on one page, add the following PHP code to your functions.php file:

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

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

The above code will check to see if the page loaded isn’t the “Contact” page, and if it isn’t, then the script and stylesheet used by the Contact Form 7 plugin are deregistered. Simply replace the page name that I have in the above code with the page that has your contact form. Of course, you can extend the if statement to include multiple pages as well.

I have used similar code to prevent other unnecessary scripts and stylesheets from load on every page from other plugins. You may not be able to disable some of the scripts or stylesheets, but if you can get most of them, your blog should load up quicker.

You may also like:

Wordpress Logo

New WordPress Theme – Slow Performance

Wordpress Logo

How to Prevent Scripts from Loading for Logged in Users in WordPress

Wordpress Logo

Correcting ThickBox Path in WordPress

Wordpress Logo

3 WordPress Plugins to Stop Spam

29 people had something to say about “How To Load the Contact Form 7 Script for a Contact Page Only”:

Comments


  1. Nice hack. Will definitely help reduce the page load time of other pages that ain’t the contact page.

    I’ll be looking at applying this to some plug-ins on my site that are supposed to be loaded only on posts and not site wide ;-)
    Udegbunam Chukwudi recently posted…Free Blog Setup Services For Five Grab Yours NOW!My Profile


    • I’ve been doing the same – reducing when plugin scripts run to avoid any unnecessary site wide loading.


      • It just added the hack to my blog and it works. Any ideas how to add it to other pages like my Advertise Here page where I also have the contact form showing?

        Thanks man ;-)
        Udegbunam Chukwudi recently posted…Show Adsense To Search Engine Visitors With JavaScriptMy Profile


        • You can use the ID, page title, or the post slug of the page in an array, such as:

          // Use the post slug
          if ( !is_page(array('advertise-here', 'contact') ) ) {
            wp_deregister_script( 'contact-form-7' );
          }

          or


          // Use the page title
          if ( !is_page(array('Advertise Here', 'Contact') ) ) {
            wp_deregister_script( 'contact-form-7' );
          }


  2. Thanks for this tip Paul. But I want to restrict the page numbers plugin too like this, to make that load on the index of my blog what’s the name that I should insert ? plain index.php ? or something else ?
    Mani Viswanathan recently posted…The ingenious “Plan B” Approach – Secondary Calls-To-ActionMy Profile


    • Here are two options that enable a script for the home page only, or disable the script for the home page only:


      // Removes script from all but the home page of your blog
      if ( !is_front_page() ) {
        wp_deregister_script( '[script handle]' );
      }

      or


      // Removes script from the home page of your blog
      if ( is_front_page() ) {
        wp_deregister_script( '[script handle]' );
      }


  3. This is a great looking theme BTW. What are you using? I tried to check your .css file, but you’ve removed comments and compressed it.

    From following the comment thread, does this hack work? I want to confirm before I give this a try. Loading java for contact form 7 on every page is pretty annoying.

    Thanks,

    Jarret
    Jarret recently posted…Commentluv enabled Health and Fitness blogMy Profile


    • I designed the theme myself, and the compressed CSS file is done by the W3 Total Cache plugin – the best cache plugin I have used.

      I haven’t had any issues with the hack. It does load the scripts on the contact page without a problem, but prevents it from loading on the remaining pages.


  4. 1. I didn’t know Contact Form was loading on every page.
    2. Thanks for this little rundown
    3. Pages are loading even quicker now!


    • I’m glad your pages are loading faster now that the Contact Form 7 script only loads when it needs to.


  5. Thanks for the tip!

    I used Contact Form 7 in my client’s site, will definitely try this tut.

    I reckon the js of contact form 7 is separated if we use pagespeed to measure how fast our sites load.
    Kimi recently posted…Free Thesis 18 Skins WordPressMy Profile


    • Yes, it would be counted as a separate file by any speed measuring program.


  6. I used your code and it’s work, but I want to optimize my page: on the contact page, contact-form load his script (script.js), jquery and jquery again from wp-includes. My theme already include jquery, so I would to load only script.js.
    If I use wp_deregister_script( ‘jquery’ ); it deregister all three scripts, and if I edit wp_enqueue_script( ‘contact-form-7′, wpcf7_plugin_url( ‘scripts.js’ ),
    array( ‘jquery’, ‘jquery-form’ ), WPCF7_VERSION, $in_footer ); removing ‘jquery’ it doesn’t change anything.
    Any ideas?

    ps: sorry for my english :)


  7. Sorry, I just realized that one script is jquery.form and not jquery, so I suppose I need it. The problem is only the jquery file from wp-includes.


  8. Ok, I need to cancel the jquery dependence also from wp_register_script( ‘jquery-form’…
    Ciao!


  9. Exactly what I was desperately looking for!

    It works great, thank you for this little hack which speeds up pages.


    • No problem. I’m glad you found what you were looking for.


  10. Interesting. Thanks a lot for sharing this. It’s a very informative post.
    Andrew Walker recently posted…goHastings.com Coupon CodeMy Profile


  11. Thanks for sharing this great piece of code. I always wondered on how to this with contact form 7. Thanks.
    Peter recently posted…conference callMy Profile


  12. Lovely fix, I’ve been searching for this fix for a quite while now.
    kristeen recently posted…autoverzekering berekenenMy Profile


    • I’m glad I could help you solve a problem.


  13. This is exactly what I need Paul, this is such a helpful one. I use to have contact page only so that I can identify frequently those person who wants to contact me.
    Chong Davel recently posted…Pet Food BrandsMy Profile

Do you have something to say? Let everyone know!

Commenting policy: All comments are moderated for spam. You must use your real name and not your website name or keywords. If a comment is deemed to be spam, then it will be deleted or edited. Links to your website within the comment body is not permitted, but you are free to use CommentLuv to add a link to your latest post. If you wish to add a link to your website, you can always contact me about submitting a guest post.






CommentLuv badge
This blog uses premium CommentLuv which allows you to put your keywords with your name if you have had 5 approved comments. Use your real name and then @ your keywords (maximum of 3)

Previous Post:

Next Post: