[Contact Form 7] How to set Recaptcha.js so that it does not load on anything other than the contact page.

avatar
Share This:

I am creating a contact form using Contact Form 7. I use Recaptcha to prevent spam, but because of this, Recaptcha.js always comes up in Pagespeed Insights in “Eliminate render-blocking resources” or in “Reduce unused JavaScript”. Therefore, I researched and implemented a method to prevent Recaptcha.js from loading on pages other than the contact page. Try putting the following code in functions.php.

function block_recaptcha_badge() {
 if ( !is_page( array( 'contact-me' ) ) ) {
 wp_dequeue_script( 'google-recaptcha' );
 wp_deregister_script( 'google-recaptcha' );
 add_filter( 'wpcf7_load_js', '__return_false' );
 add_filter( 'wpcf7_load_css', '__return_false' );
 }
}
add_action( 'wp_print_scripts', 'block_recaptcha_badge' );

If you have multiple contact form pages, you can increase the array(‘contact-me’ part, like below.

array( ‘contact-me’, ‘contact-me’, ‘contact-me-3’

Comments

Leave a Reply

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

sixteen − eight =