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

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

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.

Unlimited data backup with BackBlaze

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

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.

34 Responses to “How To Load the Contact Form 7 Script for a Contact Page Only”

  1. Carol Ann says:

    I also tried using (array(‘slug’))

  2. Carol Ann says:

    This is a great post! Only problem is it is not working for me 🙁 I’ve done everything you have said and still no luck. Below is a copy of the code i’m using. Any chance you could take a look?

    /** Prevent Contact Form 7 from loading on every page */
    add_action( ‘wp_print_scripts’, ‘my_deregister_javascript’, 100 );
    function my_deregister_javascript() {
    if ( !is_page(‘gift-advertising’,’Gift-Idea-Banners’,’sponsor-a-giveaway’,’gift-idea-review’,’gift-of-the-month-sponsor’,’sponsored-gift-post’) ) {
    wp_deregister_script( ‘contact-form-7’ );
    add_action( ‘wp_print_styles’, ‘my_deregister_styles’, 100 );
    function my_deregister_styles() {
    if ( !is_page(‘gift-advertising’,’Gift-Idea-Banners’,’sponsor-a-giveaway’,’gift-idea-review’,’gift-of-the-month-sponsor’,’sponsored-gift-post’) ) {
    wp_deregister_style( ‘contact-form-7’ );

    • Johnny Guest says:

      Great tip! Unfortunately, I am having the same problem. I entered the code just as Carol Ann did, but to no avail. I only want the Contact Form 7 code to appear on the “Contact Us” page, so I have the following for deregistering both javascript and styles:

      if ( !is_page(‘contact-us’) ) {

      But the Contact Form 7 stuff still appears under every post where we want comments; in particular:

      I am using the Super-Skeleton theme and Akismet. If anyone could help, it would be greatly appreciated. I’ve scoured the Interwebs for solutions, and nothing seems to work. I’m pulling my hair out. Thanks in advance!

  3. lisa says:

    Thanks for introducing a little rationality into this dabete.

  4. Chong Davel says:

    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.

  5. kristeen says:

    Lovely fix, I’ve been searching for this fix for a quite while now.

  6. Peter says:

    Thanks for sharing this great piece of code. I always wondered on how to this with contact form 7. Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.