0

"Unknown email address. Check again or try your username."

I understand a user can log in with either username or email. But when username is an email (and particularly different to the actual email) I think it freaks out with above message. I can imagine if it finds an @ symbol it presumes its an email and only looks up email and not username. In my testing, every user that has a different username email to primary email this is a problem. Is there a snippet I can insert to capture this?

3
  • Instead of trying to find a solution for a problem that shouldn't exist in the first place, why not allow email formats in nickname. How do you even have a nickname as a email, was that on purpose? Feb 2, 2022 at 7:15
  • For context; our website distributes google datastudio dashboards. A user registers with a primary email that by default becomes their username. This is the email we would ordinarily contact them with. If it's not a google email then they can change the email (not username) to add a second email. This is also used for google apps SSO and synced to an external CRM. It's quite complex but this turned out to be the simplest way to join all the dots... except for this strange little issue.
    – Mike
    Feb 2, 2022 at 9:48
  • This website suggests it should simple move through each authentication method (username/password then email/password) active-directory-wp.com/docs/Technical_details/…. But that's not what is happening when username is an email. When username is not an email it works fine with login as username or email.
    – Mike
    Feb 2, 2022 at 9:49

1 Answer 1

0

The answer is this: to disable standard authentication by email and update username by first attempting to looking up email.

remove_filter( 'authenticate', 'wp_authenticate_email_password', 100 );
add_action('wp_authenticate','login_verification'); 

function login_verification($username) { 
    $username = sanitize_user( $username );
    //check if this is an email 
    $user = get_user_by('email',$username);  
    if(!empty($user->user_login)) {  
        $username = $user->user_login; 
    }
    //else just leave as username
    return $username;  
}
2
  • That sounds like a reasonable approach, but I don't think that will work. There's no filter for just the username like that I don't think. You also have to match the priority of the filter you're removing, which was 20 not 100.
    – Rup
    Feb 4, 2022 at 12:09
  • Instead I'd probably remove both wp_authenticate_email_password and wp_authenticate_username_password and replace them with your own version that processes both in the right order, whether the string passes is_email() or not. I guess they were separated to allow turning off email login with a filter but I'm not convinced it's a very clean separation.
    – Rup
    Feb 4, 2022 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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