Load Contact Form 7 reCAPTCHA Only on Form Pages
Stops the Google reCAPTCHA script that Contact Form 7 adds to every page from loading where no form is shown, which speeds up the rest of the site and hides the badge there.
Opens WPIDE on
Code
$wpide_cf7_recaptcha_pages = array(); // Extra page IDs that show a form outside the content.
add_action( 'wp_enqueue_scripts', function () use ( $wpide_cf7_recaptcha_pages ) {
$post = get_post();
$uses = $post && is_singular() && ( has_shortcode( $post->post_content, 'contact-form-7' ) || has_block( 'contact-form-7/contact-form-selector', $post ) );
if ( $uses || ( $wpide_cf7_recaptcha_pages && is_page( $wpide_cf7_recaptcha_pages ) ) ) {
return;
}
wp_dequeue_script( 'wpcf7-recaptcha' );
wp_dequeue_script( 'google-recaptcha' );
}, 50 );
Notes
Add page IDs to the list if a form appears outside the post content, for example in a footer widget.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.