【问题标题】:WordPress Filter: wp_authenticate_user not getting user dataWordPress 过滤器:wp_authenticate_user 未获取用户数据
【发布时间】:2019-01-28 15:30:34
【问题描述】:

对于 WordPress + WooCommerce 设置,我正在尝试使用wp_authenticate_user 过滤器在登录时实现电子邮件激活和 Google Captcha 功能,但检查这些的顺序是错误的。

好的场景

  1. Blank username and password without Captcha submit > 得到正确的错误提示密码为空

  2. 无效的用户名没有密码和验证码提交 > 更正错误消息说错误的用户名或密码

  3. 验证码提交时密码错误的有效用户名 > 用户名或密码错误

糟糕的情况

  1. 有效的用户名和错误的密码没有验证码提交> 验证码错误 (预计用户名或密码错误)。

如何在用户名和密码验证后更改此项以检查验证码?

注意:

如果我将电子邮件激活检查切换为具有更高优先级,那么我会在糟糕的情况下收到该错误。

验证码检查

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


    【解决方案1】:

    验证用户名/电子邮件的函数与优先级为20autenticate 过滤器挂钩。并且钩子是通过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(或更高的值 - 2130等)作为优先级:

    add_filter( 'authenticate', 'verify_login_captcha', 21, 3 );
    add_filter( 'authenticate', 'custom_authenticate_user', 21 );
    

    并更改您的函数声明,使其看起来像这样,其中第一个参数要么NULLWP_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;
      }
    
      ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-17
      • 1970-01-01
      • 2021-08-29
      • 1970-01-01
      相关资源
      最近更新 更多