Optimize the Contact Form 7 plugin for Thesis (Updated)

There are thousands of contact form plugins out there. Hell, you can even roll your own if you want to. But I’m lazy. And I want to focus on other things. So this is one of the few areas that I use a plugin. I’ve found that Contact Form 7 is the easiest to set up and configure. There was only one problem with it: the javascript loaded on every page. And being anal about things, I only wanted it on the actual contact page. So I decided to write a quick function to fix that, without hacking the actual plugin.

A few notes:

  • This assumes that you have everything in the default folders
  • This is based on Thesis v1.8, and CF7 v2.3 updated for v2.4.5
/* Adds in scripts for contact form 7 */
	remove_action('wp_enqueue_scripts', 'wpcf7_enqueue_scripts' ); // Removes the scripts from loading on all pages for CF 7
	add_action('thesis_hook_footer','new_cf7_scripts'); // Adds back in the scripts on the appropriate page

function new_cf7_scripts() {
	if (is_page(array('contact','another-page'))) { // Change the name to match the name(s) of the pages using the form ?>
    <script src="<?php bloginfo('home'); ?>/wp-content/plugins/contact-form-7/jquery.form.js" type="text/javascript"></script>
	<script src="<?php bloginfo('home'); ?>/wp-content/plugins/contact-form-7/scripts.js" type="text/javascript"></script>
<?php } }

Not terribly difficult. Just drop that into your custom-functions.php file and you should be good to go.

{ 20 comments… read them below or add one }

Dr. Lawrence Kindo October 8, 2010 at 1:04 pm

Thanks for this tutorial. I have a phobia for creating contact forms and particularly with Thesis theme. I will definitely try this on my blog.

Reply

Jim October 9, 2010 at 9:34 am

Thanks Andrew, this may be more useful than you think: I have had trouble getting a newsletter sign up to submit and narrowed it down to the Contact 7 Javascript on the newsletter subscription page causing the problem. I implemented your fix and hey presto! it now works.
Regards
Jim

Reply

Jacky Liang October 11, 2010 at 5:42 pm

The script only does is move the code to the footer. I have 2 pages, still it won’t work. Any suggestions?

Reply

Norcross October 12, 2010 at 12:06 am

You’ll want to set the two page names as an array. I’ve made a quick modification to the post to show that.

Reply

Anuj March 13, 2011 at 4:15 pm

well i am looking for that problem javascript loaded on every page now i find solution thanks :)

Reply

Prem @ Indiaspider April 15, 2011 at 11:26 pm

Wow…. I looking For this Kinda Tutorial…Thanx A Lot

Reply

Sandeep@Curious Little Person April 23, 2011 at 6:42 am

Hey There,

Excellent.. i just tried this code on my blog and works like a charm.. I guess now with this code, the contact form loads in the contact page and not while loading the home page..

Do you mind if i share this code with my readers and give your site credit… i meant this is a must have to reduce site loading time.. and the best part is that its a code and no frickin plugin

Reply

Norcross April 23, 2011 at 9:36 am

Sure, feel free.

Reply

Sandeep@Curious Little Person April 23, 2011 at 11:49 am

thks

Reply

Anthony June 4, 2011 at 5:52 pm

just been working on a WP site with thesis for a client, just downloaded latest version of the plugin and the tweak didn’t work quite right, it wasn’t removing the scripts on other pages but still added the extra code on the contact pages. While digging around the plugin code I have found that they have changed where the JS is hooked in from the original ‘wp_print_scripts’ to: ‘wp_enqueue_scripts’

changing that one reference in your tweak resolved the issue for me

Reply

Norcross June 6, 2011 at 4:00 am

Thanks for the heads up. It appears as though they made an update to how the scripts are triggered. I’ll take a look and update the tutorial.

Reply

Robert Tanner August 2, 2011 at 8:17 am

Andrew,

Thanks for posting this function. It worked immediately on my site.

Regards,

Robert

Reply

arahman August 10, 2011 at 2:11 pm

Is there a way to exclude certain pages instead of “if (is_page(array(‘contact’,'another-page’)))” maybe by using an else? basically, I have a site and it uses contact form 7 on all pages but 3, so instead of adding all 30 or so pages and leaving out the 3 that do not use contact form 7….

I know 3 pages should not bog down my load time that much, but just wondering if there is such a solution.

Thanks

Reply

Norcross August 10, 2011 at 2:13 pm

yep. instead of if (is_page(.... add an exclamation point if (!is_page(.... and just list the page slugs you want to exclude.

Reply

Bradley December 15, 2011 at 5:50 am

Thanks Norcross! This is a common problem with contact forms so it awesome to find simple solutions, thanks again.

Reply

Paul December 20, 2011 at 6:29 am

Wouldn’t it be better to put this code in the plugin itself (wp-contact-form-7.php?This way if the user ever changed themes, the effect would still occur – the javascript would only load on the correct page.

It seems that this should be suggested to the plugin author – offer an option to restrict it to certian pages…

Reply

Norcross December 20, 2011 at 7:43 am

No, because if you put it in the plugin file, it’ll get wiped out whenever the plugin upgrades.

Reply

Paul December 21, 2011 at 6:40 am

And putting it in the theme it will get wipe out when the theme is upgraded….

Reply

Mike Taber December 23, 2011 at 12:39 pm

Thanks, this was helpful. I did make some modifications that might prove useful for others though. In addition to the scripts that contact-7 loads, there is also a stylesheet that can be excluded. Depending on your host and what else you have going on, it might make a difference. Here are my modifications in full:

/* Adds in scripts for contact form 7 */
remove_action(‘wp_enqueue_scripts’, ‘wpcf7_enqueue_scripts’ ); // Removes the scripts from loading on all pages for CF 7
remove_action(‘wp_enqueue_scripts’, ‘wpcf7_enqueue_styles’ ); // Removes the styles from loading on all pages for CF 7
add_action(‘thesis_hook_footer’,'new_cf7_scripts’); // Adds back in the scripts on the appropriate page

function new_cf7_scripts() {
// Change the name to match the name(s) of the pages using the form
if (is_page(array(‘contact-me’)))
{
wp_enqueue_style( ‘contact-form-7′, wpcf7_plugin_url( ‘styles.css’ ), array(), WPCF7_VERSION, ‘all’ ); ?>
<script src="/wp-content/plugins/contact-form-7/jquery.form.js” type=”text/javascript”>
<script src="/wp-content/plugins/contact-form-7/scripts.js” type=”text/javascript”>
<?php }
}

Reply

Ferb January 27, 2012 at 7:37 am

Hi Andrew,

It’s alright if you were lazy, but at least it helps me with the contact form 7, great.

Reply

Leave a Comment

Previous post:

Next post: