How to Add Google reCAPTCHA to Contact Form 7 in WordPress?

Google reCAPTCHA is a free service that helps to protect websites from spam and abuse. Contact Form 7 is a popular plugin used to create contact forms in WordPress. By default, Contact Form 7 includes Google reCAPTCHA on all forms. However, you may want to show reCAPTCHA only on the contact page to prevent spam. In this article, we’ll show you how to do just that.

Step 1: Install and Activate Contact Form 7

If you haven’t already, you’ll need to install and activate the Contact Form 7 plugin. To do this, go to the Plugins section of your WordPress dashboard and click “Add New”. In the search bar, type “Contact Form 7” and click “Install Now” on the Contact Form 7 plugin. Once it’s installed, click “Activate”.

Step 2: Set Up reCAPTCHA on Google

Next, you’ll need to generate reCAPTCHA keys for your website. This is necessary to use Google reCAPTCHA. To do this, go to the reCAPTCHA website and sign in with your Google account. After signing in, you’ll be prompted to register a new website. Enter your website’s domain and select “reCAPTCHA v3”. After agreeing to the terms of service, you’ll be given a site key and secret key.

Step 3: Integrate reCAPTCHA with Contact Form 7

To integrate reCAPTCHA with Contact Form 7, go to Contact > Integration > reCAPTCHA in your WordPress dashboard. In the reCAPTCHA section, select the type of reCAPTCHA you want to use (v2 or v3), and enter your site key and secret key.

Once you have entered the necessary information, click “Save Changes”. You have successfully added reCAPTCHA to your Contact Form 7 form.

Step 4: Remove Google reCAPTCHA badge on other pages

To only display the reCAPTCHA on the contact page, you’ll need to add some custom functionality to your website. Add the following code to your theme’s functions.php file:

function wpd_remove_recaptcha_badge() {
	if ( !is_page( array( 'contact' ) ) ) {
		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', 'wpd_remove_recaptcha_badge' );

This code will only enqueue the Google reCAPTCHA script if the page being viewed has the slug “contact”. If you want to use a different page slug, change “contact” in the is_page() function to the slug of the page you want to use. You can add even more than one slug.

That’s it! You’ve successfully added Google reCAPTCHA to your Contact Form 7 form and ensured that it only appears on the contact page to prevent spam.

Leave a Comment

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