Custom failed login error messages for users based on user role?

Is there a way to give users that belong to a certain user role a unique error after failing to login?Basically, I impor

Is there a way to give users that belong to a certain user role a unique error after failing to login?

Basically, I imported users from a site with a different CMS and it was near impossible to import their passwords. I would like to have the users of the older site belong to a specific user role to get an error saying due to the redevelopment of the website, they need to reset their passwords for security purposes.

Is there a way to give users that belong to a certain user role a unique error after failing to login?

Basically, I imported users from a site with a different CMS and it was near impossible to import their passwords. I would like to have the users of the older site belong to a specific user role to get an error saying due to the redevelopment of the website, they need to reset their passwords for security purposes.

Share Improve this question asked Apr 26, 2020 at 19:39 Michael R. BurkeMichael R. Burke 33 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can hook to the wp_login_errors filter.

add_filter('wp_login_errors', function($errors, $redirect_to){

    if(isset($errors->errors['incorrect_password']) &&
    in_array('custom_role', (array) get_user_by('login', $_POST['log'])->roles)){
      $errors->errors['incorrect_password'][0] = 'Custom message';
    }

  return $errors;
}, 10, 2);

EDIT:

This should work with WooCommerce login form or any other form that implements wp_signon:

add_filter('authenticate', function($user, $username, $password){

    if(is_wp_error($user) && 
    isset($user->errors['incorrect_password']) &&
    in_array('custom_role', (array) get_user_by('login', $username)->roles)){
        $user->errors['incorrect_password'][0] = 'Custom message';
    }
    return $user;

}, 21, 3);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744524604a4578782.html

相关推荐

  • Custom failed login error messages for users based on user role?

    Is there a way to give users that belong to a certain user role a unique error after failing to login?Basically, I impor

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信