I'm having a problem with adding Recaptcha v3 to my WP registration form.
The recaptcha seems to be registered and working and communicating with Google's api. I'm running into problems when I try to fire a PHP function that checks the Recaptcha score which hooks into 'registration_errors'. See the code below:
function verify_recaptcha_on_register($errors, $sanitized_user_login, $user_email) {
$recaptcha_url = '';
$recaptcha_secret = 'SECRET KEY';
$recaptcha_response = $_POST['recaptcha_response'];
// Make and decode POST request:
$recaptcha = file_get_contents($recaptcha_url . '?secret=' . $recaptcha_secret . '&response=' . $recaptcha_response);
$recaptcha = json_decode($recaptcha);
$score = $recaptcha->score;
// Take action based on the score returned:
if ($recaptcha->score > 0.5) {
// Verified -
return $errors;
} else {
// Not verified - show form error
return new WP_Error( 'Captcha Invalid', __('<strong>ERROR</strong>: You are a bot') );
}
}
add_filter('registration_errors', 'verify_recaptcha_on_register', 10, 2);
The $_POST['recaptcha_response']
gets its value from a hidden input field in the registration form, which gets a token value from the Recaptcha script (I've checked, and this part works).
When I submit the form, I'm getting the following result: "The site is experiencing technical difficulties. Please check your site admin email inbox for instructions."
I'm not sure if I have a minor mistake or this is actually entirely the wrong way to go about this. Any help would be appreciated.