0

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.

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);
5
  • This works great! Thank you so much! Now, if I can just find the filter to hook it to the WooCommerce login form... – Michael R. Burke Apr 28 '20 at 5:06
  • The second filter I added should work. @MichaelR.Burke – Himad Apr 28 '20 at 11:35
  • You're awesome, brotha! I really should get used to messing around with filters more. – Michael R. Burke Apr 28 '20 at 21:04
  • Please select my answer if it solved your issue. Glad to help :) – Himad Apr 29 '20 at 10:40
  • You got it... consider it done! :) – Michael R. Burke Apr 29 '20 at 22:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.