How to Optionally Disable Contact Form 7 & Google Recaptcha for Pages Without Forms

First, you’ll want to add these two lines to your website’s wp-config.php file that is found in the root folder.

/** Contact Form 7: disable asset loading on frontend */
define('WPCF7_LOAD_JS', false);
define('WPCF7_LOAD_CSS', false);

The rest of the code snippets go in your active WordPress theme’s functions.php file.

This first snippet is just a simple function that checks the content of your single post, page, or custom post type for the

Error: Contact form not found.shortcode and returns a boolean true or false if detected or not. You can also pass it a post ID to check when outside the loop. Otherwise, it relies on the global $post object.

/**
 * Check if Contact Form 7 Shortcode Exists
 * 
 * Only checks content for the `[contact-form-7]` shortcode in 
 * singular post types. Defaults to false for other templates.
 * 
 * @param int|null $post_id Optional. Post ID to check, otherwise 
 * it grabs it from the global `$post` object.
 * @return bool True if shortcode was found. False otherwise.
 */
function fc_cf7_shortcode_exists($post_id = null)
{
    if (!is_null($post_id) || (is_singular() && class_exists('WPCF7'))) {
        if (is_null($post_id)) {
            global $post;
            $post_id = $post->ID;
        }
        return strpos(get_post_field('post_content', $post_id), '[contact-form-7 ') !== false;
    }
    return false;
}

This second snippet utilizes the above function to optionally disable Google Recaptcha if implemented through Contact Form 7’s integration settings. This function is called in wp_enqueue_scripts with a high priority because Contact Form 7 adds the action right away in their plugin (found in /wp-content/plugins/contact-form-7/modules/recaptcha/recaptcha.php:17).

/**
 * Disable Contact Form 7's Google Recaptcha when form is not on page
 */
add_action('wp_enqueue_scripts', function () {
    if (!tw_cf7_shortcode_exists()) {
        remove_action('wp_enqueue_scripts', 'wpcf7_recaptcha_enqueue_scripts', 20, 0);
    }
}, 1, 0);

This third and final snippet also uses our first function to optionally enable Contact Form 7’s stylesheets (CSS) and JavaScript (JS). It’s hooking into the same action as our previous snippet, but now with a later priority since Contact Form 7 registers their scripts at the default 10 (found in /wp-content/plugins/contact-form-7/includes/controller.php).

/**
 * Enable Contact Form 7's assets when form is on page
 */
add_action('wp_enqueue_scripts', function () {
    if (tw_cf7_shortcode_exists()) {
        if (function_exists('wpcf7_enqueue_scripts')) {
            wpcf7_enqueue_scripts();
        }
        if (function_exists('wpcf7_enqueue_styles')) {
            wpcf7_enqueue_styles();
        }
    }
}, 20, 0);

Putting All the Snippets Together

wp-config.php

/** Contact Form 7: disable asset loading on frontend */
define('WPCF7_LOAD_JS', false);
define('WPCF7_LOAD_CSS', false);

Active WordPress Theme’s functions.php

/**
 * Check if Contact Form 7 Shortcode Exists
 * 
 * Only checks content for the `[contact-form-7]` shortcode in 
 * singular post types. Defaults to false for other templates.
 * 
 * @param int|null $post_id Optional. Post ID to check, otherwise 
 * it grabs it from the global `$post` object.
 * @return bool True if shortcode was found. False otherwise.
 */
function tw_cf7_shortcode_exists($post_id = null)
{
    if (!is_null($post_id) || (is_singular() && class_exists('WPCF7'))) {
        if (is_null($post_id)) {
            global $post;
            $post_id = $post->ID;
        }
        return strpos(get_post_field('post_content', $post_id), '[contact-form-7 ') !== false;
    }
    return false;
}

/**
 * Disable Contact Form 7's Recaptcha when form is not on page
 */
add_action('wp_enqueue_scripts', function () {
    if (!tw_cf7_shortcode_exists()) {
        remove_action('wp_enqueue_scripts', 'wpcf7_recaptcha_enqueue_scripts', 20, 0);
    }
}, 1, 0);

/**
 * Enable Contact Form 7's assets when form is on page
 */
add_action('wp_enqueue_scripts', function () {
    if (tw_cf7_shortcode_exists()) {
        if (function_exists('wpcf7_enqueue_scripts')) {
            wpcf7_enqueue_scripts();
        }
        if (function_exists('wpcf7_enqueue_styles')) {
            wpcf7_enqueue_styles();
        }
    }
}, 20, 0);

Experience the Power of Preinstalled OS Drives and Booting Guides
Logo
Compare items
  • Total (0)
Compare
0
Ninja Silhouette 9 hours ago

Joe Doe in London, England purchased a

Joe Doe in London?

Joe Doe in London, England purchased a

Joe Doe in London?

Joe Doe in London, England purchased a

Joe Doe in London?

Joe Doe in London, England purchased a