How to Disable reCAPTCHA for Automated Testing

Overview

Are you using automated testing on your site and need to disable reCAPTCHA during the test? More and more sites these days are turning to automated testing for their site. With reCAPTCHA enabled, these automated tests will fail and so it may become necessary to disable reCAPTCHA for forms while the testing runs through. In this tutorial, we’ll show you the filters you need to disable these settings for your automated testing.

Setup

You’ll need to add this code snippet to your site. If you need help in adding snippets to your site, please see this tutorial.

/**
 * Disable for automated testing.
 *
 * @link   https://wpforms.com/developers/how-to-disable-recaptcha-for-automated-testing/
 */

    // Disable reCAPTCHA assets and initialisation on the frontend.
    add_filter( 'wpforms_frontend_recaptcha_disable', '__return_true' );

    // Disable validation and verification on the backend.
    add_filter( 'wpforms_process_bypass_captcha', '__return_true' );

Alternatively, you can also disable using this snippet.

And that’s it! Would you like to also change the theme of reCAPTCHA? Take a look at our article on How to Change the Captcha Theme on Google Checkbox v2 reCAPTCHA.

FAQ

Q: Can I disable this for users who are logged-in?

A: Absolutely! Just use this snippet.

/**
 * Disable CAPTCHA for all logged-in users
 *
 * @link   https://wpforms.com/developers/how-to-disable-recaptcha-for-automated-testing/
 */

// Skip CAPTCHA for logged in users only.
add_action( 'plugins_loaded', static function() {

	// Check if the current visitor is a logged in user.
	if ( is_user_logged_in() ) {
		add_filter( 'wpforms_process_bypass_captcha', '__return_true' );
		add_filter( 'wpforms_frontend_recaptcha_disable', '__return_true' );
		remove_action( 'wpforms_frontend_output', [ wpforms()->get( 'frontend' ), 'recaptcha' ], 20 );
	}
}, 11 );