0

Happy New Year!

Just wondering if anyone knows how can we user Google reCaptcha on WP login page?

Either v2 or v3

2
  • There are existing plugins to do that. I haven't used any to recommend them though.
    – Rup
    Jan 3 '20 at 2:29
  • Thank you - I will look it up Jan 3 '20 at 4:45
0

Happy new year.

Here is plugin for this : https://wordpress.org/plugins/login-recaptcha/

Please check. i am sure you can get your requirements are fulfill.

0

For a plugin, definitely consider Login No Captcha reCAPTCHA by Robert Peake.

If you're looking to roll something into your theme without introducing another plugin to the mix, Megan at Megabyte created an article.

<?php
/**
* These Functions Add and Verify the Invisible Google reCAPTCHA on Login
*/

add_action('login_enqueue_scripts', 'login_recaptcha_script');

function login_recaptcha_script() {
wp_register_script('recaptcha_login', 'https://www.google.com/recaptcha/api.js');


wp_enqueue_script('recaptcha_login')


}



add_action( 'login_form', 'display_recaptcha_on_login' );

function display_recaptcha_on_login() {
echo "<script>
function onSubmit(token) {
document.getElementById('loginform').submit();
}
</script>
<button class='g-recaptcha' data-sitekey='YOUR_PUBLIC_KEY' data-callback='onSubmit' data-size='invisible' style='display:none;'>Submit</button>";


}



add_filter('wp_authenticate_user', 'verify_recaptcha_on_login', 10, 2);

function verify_recaptcha_on_login($user, $password) {

if (isset($_POST['g-recaptcha-response'])) {
$response = wp_remote_get( 'https://www.google.com/recaptcha/api/siteverify?secret=YOUR_SECRET_KEY&response=' . $_POST['g-recaptcha-response'] );


$response = json_decode($response['body'], true);


if (true == $response['success']) {


return $user;


} else {


// FIXME: This one fires if your password is incorrect... Check if password was incorrect before returning this error...


// return new WP_Error( 'Captcha Invalid', __('<strong>ERROR</strong>: You are a bot') );


}


} else {


return new WP_Error( 'Captcha Invalid', __('<strong>ERROR</strong>: You are a bot. If not then enable JavaScript.') );


}


}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.