我真的想在wp登录页面上实现Google的CaptchaV3。我在不同的地方看到过几次引用这个片段。问题是,似乎没有人提到如何检查“//FIXME”部分:如果您的密码不正确,此部分将触发……在返回此错误之前,请检查密码是否不正确……”
是否可以使用ajax检查用户的密码是否正确?
这将是WP开发的巨大资产。
/**
* 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.\') );
}
}