Disable password recovery for a specific user role
-
Users with a role of Subscriber on our site should not be able to reset their password in the frontend Recover Password Form.
I have added code suggested elsewhere to prevent password resets for those with the Subscriber user role shown below, but the email is still sending. So I am wondering if there are different hooks I can use with this plugin to prevent the email from sending and returning an error to explain Subscribers can’t reset their password.
add_filter('allow_password_reset', function ($allow, $ID) {
if ((! $allow) || is_wp_error($allow)) {
// Rejected by a previous filter
return $allow;
}
// Read the user's roles
$user_data = get_userdata($ID);
// subscriber users may not reset passwords
if (in_array('subscriber', (array) $user_data->roles)) {
return false;
}
return true;
}, 10, 2);The email that is sent returns a link that correctly doesn’t have a valid key, meaning the password recovery won’t work but the link is structured like this: https://example.test/password-recovery/?key%5Berrors%5D%5Bno_password_reset%5D%5B0%5D=Password reset is not allowed for this user&&&login=charliedek.
Any suggestions are appreciated!
- You must be logged in to reply to this topic.