Are you running automated tests on your website? When reCAPTCHA is enabled, automated tests will fail since they can’t solve the CAPTCHA challenge.
This guide will show you how to temporarily disable reCAPTCHA during automated testing sessions.
Disabling reCAPTCHA
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.
This code:
- On line 8: Prevents reCAPTCHA from loading on your forms
- On line 11: Bypasses the CAPTCHA verification when processing form submissions
Selective Disabling for Logged-in Users
If you want to disable reCAPTCHA only for logged-in users (useful for testing while maintaining protection for public submissions), use this alternative code:
/**
* 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 );
And that’s it! Next, would you like to also change the theme of reCAPTCHA? Take a look at our article on changing the CAPTCHA theme on Google Checkbox v2 reCAPTCHA.