In its default settings, Contact Form 7 loads its JavaScript and CSS stylesheet on every page. You might think it would be redundant or wasteful, and want to load them only on those pages that contain contact forms. I understand the feeling, but there is a technical difficulty for a plugin in knowing whether the page contains contact forms or not at the start of loading. However, I can show you a way to work around that.
Step 1: Stop loading the JavaScript and CSS stylesheet on all pages
When the value of WPCF7_LOAD_JS is set to false (default: true), Contact Form 7 does not load the JavaScript. You can set the value of this constant in your wp-config.php like this:
define ('WPCF7_LOAD_JS', false);
Likewise, you can control the loading of the CSS stylesheet with WPCF7_LOAD_CSS. Contact Form 7 does not load the CSS stylesheet when the value of WPCF7_LOAD_CSS is false (default: true). You can set it in the wp-config.php like this:
define('WPCF7_LOAD_CSS', false);
Now you have succeeded in stopping the loading of the JavaScript and CSS stylesheet, but, unfortunately, you’ve also killed them on pages that contain contact forms — so we really need to get them back on the right pages. So, the next step is what you’ll need to load the files on to the explicit pages in which you need them.
Step 2: Load the files on pages which contain contact forms
For example, let’s say you have a page named “Contact” and it is the only page that contains a contact form. And suppose that you have a template file for the “Contact” page named ‘contact.php’ in your theme folder. Now you will need to load Contact Form 7’s JavaScript and CSS stylesheet specifically on the “Contact” page.
To do this, you must edit the ‘contact.php’ template file and insert the following lines into it:
<?php
if ( function_exists( 'wpcf7_enqueue_scripts' ) ) {
wpcf7_enqueue_scripts();
wpcf7_enqueue_styles();
}
?>
Note that wpcf7_enqueue_scripts() and wpcf7_enqueue_styles() must be called before wp_head() is called.
English
Japanese 

I use Form7 WP plugin
I’d like to add the adwords google counter tag on the page “Email sent ok” … How can I do this ? That will help me drive my adwords campaigns with a CPA
Thanks for your answer
David
Using the most recent version (2.0.7) of Contact Form 7 you can change a little portion of the plugin to make this more flexible using custom fields.
Just open the file
controller.phpwhich is located in the plugin’s directory underincludesand add the following code right at the beginning of the functionswpcf7_enqueue_scriptsandwpcf7_enqueue_styles, i.e. right after the curly brackets on a new line:global $post;
if (get_post_meta($post->ID, ‘cf7′, true) != ‘1′) return;
This way only posts/pages containing a custom field cf7 with its value set to 1 will load the corresponding CSS/JavaScript.
Regarding the example above you should add a custom field to the page “Contact” and the form will work on this particular page.
Looks like a nice hack. If you show a form only on a singular page (I mean, non-archive pages), this should work. Thanks Christian.
It has worked for me so far because I’ve always put my forms on a separate page. Anyway, I’d love to see something like this incorporated into the plugin – adding the code after every update is quite annoying.
Hi Takayuki,
Thanks very much for a great plugin.
I’m trying to disable the javascript and css with the lines below, in my wp-congif.php.
define (‘WPCF7_LOAD_JS’, false);
define (‘WPCF7_LOAD_CSS’, false);
Had a little stupid mistake, putting the lines right at the bottom of the config file. It has no effect unless you put it above the line below:
/* That’s all, stop editing! Happy blogging. */
Hope this helps anyone else wondering why its not working!
Thanks.
Appreciate these instructions, and the comments, especially that from Christian Schenk. I was worried about mucking with the PHP files, but your instructions were clear.
One note: make sure the single quote marks in your PHP file are _not_ smart quotes, as they appear in the comment above.
Thanks Takayuki for your continued development and improvement.
Or, you can write an extra helper plugin on your own which does once deregister the script and enqueue it again if the post has ‘cf7′ meta.
Don’t you think that this is something a lot of people might want to do? Why not build it into the plugin?
Because there is no better way than this. Note that many users put thier contact forms in posts (they will be shown on index page and archive pages) and sidebar widgets. In such cases, setting post meta doesn’t work.
Agree. Thank you for discussing this with me.