WordPress 过滤器:wp_authenticate_user 未获取用户数据
WordPress Filter: wp_authenticate_user not getting user data
对于 WordPress + WooCommerce 设置,我正在尝试使用 wp_authenticate_user
过滤器在登录时实现电子邮件激活和 Google 验证码功能,但检查这些的顺序是错误的。
好的场景
没有验证码提交的空白用户名和密码 > 得到正确的错误 密码为空。
没有密码的无效用户名和验证码提交 > 更正错误消息 错误的用户名或密码。
用户名有效,密码错误,验证码提交 > 用户名或密码错误
糟糕的场景
- 用户名有效,密码错误,未提交验证码 > 验证码错误
(预计用户名或密码错误)。
如何更改此设置以在用户名和密码验证后检查验证码?
注:
如果我将激活的电子邮件检查切换为具有更高的优先级,那么在糟糕的情况下我会收到该错误。
验证码检查
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);
验证 username/email 的函数挂钩到具有优先级 20
的 autenticate
过滤器。钩子是通过 wp-includes/default-filters.php
添加的,如下所示:
// Default authentication filters
add_filter( 'authenticate', 'wp_authenticate_username_password', 20, 3 );
add_filter( 'authenticate', 'wp_authenticate_email_password', 20, 3 );
因此,如果您希望自定义验证函数 运行 在 那些默认验证之后,那么您应该改为挂钩 authenticate
过滤器并使用 20
(或更高的值 - 21
、30
等)作为优先级:
add_filter( 'authenticate', 'verify_login_captcha', 21, 3 );
add_filter( 'authenticate', 'custom_authenticate_user', 21 );
并更改您的函数声明,使其看起来像这样,其中第一个参数是 一个 NULL
或 WP_User
成功实例:
function verify_login_captcha( $user, $username, $password ) {
...your validation...
return $user; // You should return the WP_User instance or a WP_Error instance on error.
}
function custom_authenticate_user( $user ) {
...your validation...
return $user; // You should return the WP_User instance or a WP_Error instance on error.
}
PS:确保在访问其属性和方法之前检查 $user
是否是有效的用户对象。有关详细信息,请参阅 here。 例如:
function custom_authenticate_user( $user ) {
if ( ! $user ) {
return $user;
}
...
}
对于 WordPress + WooCommerce 设置,我正在尝试使用 wp_authenticate_user
过滤器在登录时实现电子邮件激活和 Google 验证码功能,但检查这些的顺序是错误的。
好的场景
没有验证码提交的空白用户名和密码 > 得到正确的错误 密码为空。
没有密码的无效用户名和验证码提交 > 更正错误消息 错误的用户名或密码。
用户名有效,密码错误,验证码提交 > 用户名或密码错误
糟糕的场景
- 用户名有效,密码错误,未提交验证码 > 验证码错误 (预计用户名或密码错误)。
如何更改此设置以在用户名和密码验证后检查验证码?
注:
如果我将激活的电子邮件检查切换为具有更高的优先级,那么在糟糕的情况下我会收到该错误。
验证码检查
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);
验证 username/email 的函数挂钩到具有优先级 20
的 autenticate
过滤器。钩子是通过 wp-includes/default-filters.php
添加的,如下所示:
// Default authentication filters
add_filter( 'authenticate', 'wp_authenticate_username_password', 20, 3 );
add_filter( 'authenticate', 'wp_authenticate_email_password', 20, 3 );
因此,如果您希望自定义验证函数 运行 在 那些默认验证之后,那么您应该改为挂钩 authenticate
过滤器并使用 20
(或更高的值 - 21
、30
等)作为优先级:
add_filter( 'authenticate', 'verify_login_captcha', 21, 3 );
add_filter( 'authenticate', 'custom_authenticate_user', 21 );
并更改您的函数声明,使其看起来像这样,其中第一个参数是 一个 NULL
或 WP_User
成功实例:
function verify_login_captcha( $user, $username, $password ) {
...your validation...
return $user; // You should return the WP_User instance or a WP_Error instance on error.
}
function custom_authenticate_user( $user ) {
...your validation...
return $user; // You should return the WP_User instance or a WP_Error instance on error.
}
PS:确保在访问其属性和方法之前检查 $user
是否是有效的用户对象。有关详细信息,请参阅 here。 例如:
function custom_authenticate_user( $user ) {
if ( ! $user ) {
return $user;
}
...
}