How To Remove Contact Form 7 Scripts and Stylesheets – Including reCAPTCHA

Remove Contact Form 7 CSS and JS From Unwanted Pages:

If you are someone who uses Contact Form 7’s WordPress plugin, you may have noticed that the scripts and styles are loaded on each and every page which slows them down since the server has to load these on every page. Luckily there is an easy way to remove these stylesheets and scripts on the pages that don’t need them. Obviously we want these scripts and stylesheets to appear on pages that use our Contact Form 7 forms. To do this follow the steps listed below.

Step 1: Go to Your functions.php file

Open the functions.php file either through FTP or go to Appearance > Theme Editor and select the functions.php file.

Step 2: Add the Following Script to the functions.php file

function rmv_contact_form_7_scripts()
{
    global $post;

    if (!has_shortcode($post->post_content, 'contact-form-7'))
    {
        wp_dequeue_script('contact-form-7');
        wp_dequeue_style('contact-form-7');
	wp_dequeue_script( 'google-recaptcha' );
	wp_dequeue_script( 'wpcf7-recaptcha' );
    }
}

add_action('wp_enqueue_scripts', 'rmv_contact_form_7_scripts');

What this does is that it first tests whether or not if we have a Contact From 7 shortcode on the page. If a Contact Form 7 shortcode is not present on the page, it will remove, aka “dequeue”, the scripts that are not included.

This line removes the Contact Form 7 JavaScript files.

wp_dequeue_script('contact-form-7');

This line removes the Contact Form 7 CSS files.

wp_dequeue_style('contact-form-7');

These two lines will remove the reCAPTCHA scripts and stylesheets assuming that you have the reCAPTCHA option on in Contact Form 7’s options. If the option is off feel free to leave these two lines out of the code.

wp_dequeue_script( 'google-recaptcha' );
wp_dequeue_script( 'wpcf7-recaptcha' );

Other Tips

Do note this script will only remove the scripts if the shortcode is not found within the post content. If you have an Contact Form 7 shortcode elsewhere such as a newsletter sign up form in your sidebar, you will need to edit the IF statement (since in most themes the sidebar is not part of the post content).

The code below is specific to a theme I use so be sure not to copy and paste this into your functions.php file. In this particular site the sidebar has a Contact Form 7 shortcode for newsletter sign ups, so we want to make sure the scripts and stylesheets are loading if a sidebar is present. In this case I simply test to see if a sidebar is present and check to see if the shortcode is present in the post content. If neither a sidebar is present nor the shortcode in the post content, the Contact Form 7 scripts and stylesheets are removed. If your site uses one of these shortcodes outside of the post content, be sure to edit the if statement to reflect that.

function rmv_contact_form_7_scripts()
{
    global $post;

    if (!has_shortcode($post->post_content, 'contact-form-7') && 'no-sidebar' == rwmb_meta( 'iter_gioga_sidebar' ))
    {
        wp_dequeue_script('contact-form-7');
        wp_dequeue_style('contact-form-7');
	wp_dequeue_script( 'google-recaptcha' );
	wp_dequeue_script( 'wpcf7-recaptcha' );
    }
}

add_action('wp_enqueue_scripts', 'rmv_contact_form_7_scripts');