0

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 = 'https://www.google.com/recaptcha/api/siteverify';
$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.

  • Sounds like you've got a PHP fatal error happening and WP is catching it and showing you the generic error handler message, or even your host. You'll need to dig into your PHP error logs to find out what the real error is. Otherwise I can't help but notice there's no error checking in your code. It just assumes the request to Google worked, and that everything works as it's supposed to, but never attempts to check it actually did succeed – Tom J Nowell Aug 25 at 16:32
  • Thanks for the suggestions Tom. I checked the error log and found PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function verify_recaptcha_on_register(), 2 passed [some url] 3 needed I went through everything, isolating things, and now it works. Not sure what fixed it. I redid some things to be sure, so there might have been a minor error that I correct there. About checking for error/success, I also tested it for if ($recaptcha->success == true), and it works. Is it absolutely necessary to check for that? If you get no score back, doesn't that mean false/failure? – J. Stockland Aug 25 at 16:53
  • you'd have to ask recaptcha dev support about that – Tom J Nowell Aug 26 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.