Remove ReCAPTCHA recaptcha__en.js from all pages except Contact Form 7

biddleUncategorizedLeave a Comment

If you are like me, you looked for a solution to remove the Google Recaptcha javascript from all pages except your pages that have Contact Form 7 forms. For SEO you don’t need reCAPTCHA to load on your home page unless you have a form there so you can use the following code in your functions.php to remove the recaptcha__en.js from all pages except those with ContactForm 7!

add_action('wp_print_scripts', function () { global $post; if ( is_a( $post, 'WP_Post' ) && !has_shortcode( $post->post_content, 'contact-form-7') ) { wp_dequeue_script( 'google-recaptcha' ); wp_dequeue_script( 'wpcf7-recaptcha' ); } });

If you want to hide the reCAPTCHA badge on your website use this code in your CSS:

.grecaptcha-badge { visibility: hidden;
}

To add a confirmation page to your contact form 7 page add those snippet code:

<script> document.addEventListener( 'wpcf7mailsent', function( event ) { location = '/thank-you/'; }, false ); </script>