You are here: Home > Blogging > WordPress > Optimize WordPress Contact Form 7 Plugin Scripts

Optimize WordPress Contact Form 7 Plugin Scripts

WordPress has many contact form plugins. The one I use here at Tech Airlines is called Contact Form 7. It’s extremely flexible and easy to use. However, the one thing that has always bugged me was that its 3 scripts (Ajax and 2 jQuery scripts) load on every single page regardless of whether or not its required and only 3 of the pages on this site require it.

This can significantly slow down the loading time of a website. So today, I finally decided to go ahead and try to fix it.

Step 1: Open up your theme’s functions.php (Theme Functions) file.

Step 2: Insert the following code right before the closing “?>” tag.

// Add the Contact Form 7 scripts on selected pages

function add_wpcf7_scripts() {
if ( is_page(array(10,170)) )
wpcf7_enqueue_scripts();
}

// Remove Contact Form 7 scripts elsewhere

if ( ! is_admin() && WPCF7_LOAD_JS )
remove_action( 'wp_print_scripts', 'wpcf7_enqueue_scripts' );
add_action( 'wp', 'add_wpcf7_scripts' );

Step 3: Change the numbers within the array on the highlighted line above (Line 4) to the ID numbers of the pages you want the script to be activated, which would be the pages that contain a contact form. To find out the ID numbers of pages, check the end of the URL when editing the page.

Page ID WordPress

The page ID for the Tech Airlines's Terms of Use page is 187.

Step 4: Save the file and you’re done.

Removing these 3 scripts may slightly improve the loading speed of your WordPress powered site, especially for those with slow network connections.

Namecheap.com - Cheap domain name registration, renewal and transfers - Free SSL Certificates - Web Hosting

Tags: , , ,

6 Responses to " Optimize WordPress Contact Form 7 Plugin Scripts "

  1. Thanks Brian, it works! And saves loading time. I was willing to remove contact form 7 but you save it.

  2. [...] just as i write this, I continue my chronic habit around here of answering my own question … http://www.techairlines.com/2010/01/…lugin-scripts/ http://themeshaper.com/forums/topic/…n-functionsphp So there are ways to remove the scripts and [...]

  3. Optimize WordPress Contact Form 7 Plugin Scripts…

    …is 187. Step 4: Save the file and you’re done. Removing these 3 scripts may slightly improve the loading speed of your WordPress powered site, especially for those ……

  4. David says:

    Hi it’s got rid of all the jquery requests but there is still the call for the css

    /wordpress/wp-content/plugins/contact-form-7/styles.css?ver=2.2.1

    Is there a bit of code I can add in the function.php to get rid of this as well.

    Regards
    David
    David recently posted..Testing again #7My ComLuv Profile

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Have a question about commenting? Check the Commenting FAQ.

Copyright © 2009-2010 TechAirlines. All Rights Reserved.