是的,有一个authenticate 您可以在wp_authenticate 函数,并返回WP_Error 如果登录失败。
/**
* Filters whether a set of user login credentials are valid.
*
* A WP_User object is returned if the credentials authenticate a user.
* WP_Error or null otherwise.
*
* @since 2.8.0
* @since 4.5.0 `$username` now accepts an email address.
*
* @param null|WP_User|WP_Error $user WP_User if the user is authenticated.
* WP_Error or null otherwise.
* @param string $username Username or email address.
* @param string $password User password
*/
$user = apply_filters( \'authenticate\', null, $username, $password );
这正是WordPress内部验证用户名和密码的方式,这些是使用的默认过滤器:
add_filter( \'authenticate\', \'wp_authenticate_username_password\', 20, 3 );
add_filter( \'authenticate\', \'wp_authenticate_email_password\', 20, 3 );
add_filter( \'authenticate\', \'wp_authenticate_spam_check\', 99 );
我建议您将过滤器的优先级设置为10,或小于20(这是在检查用户名/密码时),以便首先运行检查。
如果测试通过,只需返回通过的第一个变量,例如:
add_filter( \'authenticate\', \'smyles_check_custom_auth\', 10, 3 );
function smyles_check_custom_auth( $user, $username, $password ){
$otp_check = false;
if( $otp_check ){
return $user;
}
return new WP_Error( __( \'OTP Check failed\' ) );
}