Load Contact Form 7 reCaptcha Contact Page Only

Load Contact Form 7 reCaptcha Contact Page Only
Load Contact Form 7 reCaptcha Contact Page Only

Learn how to optimize your WordPress site by selectively loading the Contact Form 7 reCAPTCHA script only on specific pages, such as the contact page, improving site performance and user experience.

Contact Form 7 version 5.1 and up ships with Google reCAPTCHA v3, and v3 runs in the background on every page of your site. That is the little badge sitting bottom-right everywhere you look. Useful on your contact page. Pointless on the other fifty pages that don’t have a form, where it’s just an extra Google script to download.

Here’s how to load it only where your form actually lives.

Step 1: Stop reCaptcha loading everywhere

Drop this into your theme’s functions.php. It unhooks Contact Form 7’s reCaptcha enqueue so the script stops loading site-wide.

PHP
<?php
/**
 * Remove CF7 reCaptcha from all pages.
 */
function contact_only_recaptcha() {
    remove_action( 'wp_enqueue_scripts', 'wpcf7_recaptcha_enqueue_scripts' );
}
add_action( 'init', 'contact_only_recaptcha' );

Step 2: Add it back on your contact page

Now re-enqueue it only on the pages that hold a form. Swap the slugs for your own, and add more to the array (services, support, whatever) if your form shows up in more than one place.

PHP
<?php
/**
 * Enqueue CF7 reCaptcha scripts on contact page or specific pages.
 */
function contact_only_recaptcha_checks() {
    if ( is_page( array( 'contact-us', 'contact' ) ) ) {  // Change slug to match the pages where CF7 is used.
        wpcf7_recaptcha_enqueue_scripts();
    }
}
add_action( 'wp_enqueue_scripts', 'contact_only_recaptcha_checks' );

One catch worth knowing

Current Contact Form 7 registers that reCaptcha enqueue at priority 20, not the default 10. WordPress remove_action only strips a hook when the priority matches, so on today’s CF7 your Step 1 call needs the priority spelled out: remove_action( 'wp_enqueue_scripts', 'wpcf7_recaptcha_enqueue_scripts', 20 );. Leave that 20 off and the removal quietly does nothing while the script keeps loading everywhere. Older CF7 hooked it at the default priority, which is why the plain version used to be enough.

One honest trade-off before you ship it: reCAPTCHA v3 scores visitors more accurately when it can watch them move across your whole site, so cutting it down to a single page makes its spam scoring a little blunter. If the performance win matters more to you than a razor-sharp score, that’s a fair swap. Test the form afterward, and confirm the badge really is gone from the rest of your site.

Next: Migrating from array() to Short Array Syntax [] in PHP

Leave a Comment

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


Scroll to Top