【发布时间】:2019-01-28 15:30:34
【问题描述】:
对于 WordPress + WooCommerce 设置,我正在尝试使用wp_authenticate_user 过滤器在登录时实现电子邮件激活和 Google Captcha 功能,但检查这些的顺序是错误的。
好的场景
Blank username and password without Captcha submit > 得到正确的错误提示密码为空。
无效的用户名没有密码和验证码提交 > 更正错误消息说错误的用户名或密码。
验证码提交时密码错误的有效用户名 > 用户名或密码错误
糟糕的情况
- 有效的用户名和错误的密码没有验证码提交> 验证码错误 (预计用户名或密码错误)。
如何在用户名和密码验证后更改此项以检查验证码?
注意:
如果我将电子邮件激活检查切换为具有更高优先级,那么我会在糟糕的情况下收到该错误。
验证码检查
function display_login_captcha() { ?>
<div class="g-recaptcha" data-sitekey="<?php echo get_option('captcha_site_key'); ?>"></div>
<?php }
add_action( "login_form", "display_login_captcha" );
function verify_login_captcha($user,$password) {
if (isset($_POST['g-recaptcha-response'])) {
$recaptcha_secret = get_option('captcha_secret_key');
$response = wp_remote_get("https://www.google.com/recaptcha/api/siteverify?secret=". $recaptcha_secret ."&response=". $_POST['g-recaptcha-response']);
$response = json_decode($response["body"], true);
if (true == $response["success"]) {
return $user;
} else {
return new WP_Error("Captcha Invalid", __(" Only 3 attemps allowed, Are you Human? Please validate yourself"));
}
} else {
return new WP_Error("Captcha Invalid", __(" Only 3 attemps allowed, It seems like we are having hard time identifying you as a human! If you are then enable JavaScript"));
}
}
add_filter("wp_authenticate_user", "verify_login_captcha", 10, 2);
激活检查
function custom_authenticate_user($userdata) {
$isActivated = get_user_meta($userdata->ID, 'is_activated', true);
if (!$isActivated) {
$userdata = new WP_Error(
'inkfool_confirmation_error',
__( '<strong>ERROR:</strong> 111 <'.$userdata->id.'>Your account has to be activated before you can login. You can resend by clicking <a href="/sign-in/?u='.$userdata->ID.'">here</a>', 'inkfool' )
);
}
return $userdata;
}
add_filter('wp_authenticate_user', 'custom_authenticate_user',11,1);
【问题讨论】:
标签: php wordpress woocommerce recaptcha