0

For the login, I want to enable the user to use the email instead of the username. This is what I got so far:

remove_filter('authenticate', 'wp_authenticate_username_password', 20);
function login_with_email_address($username) {
    $user = get_user_by_email($username);
    if(!empty($user->user_login))
        $username = $user->user_login;
    return $username;
}
add_action('wp_authenticate','login_with_email_address');

The result of this, is the following error message:

"Invalid username or password. Please try again!"

  • 3
    OK. So you have task and a code. What is missing? What is the problem? – Robert hue Oct 6 '15 at 6:56
  • yes @Robert hue i don't know what is missing with added code because above code i have added in function.php wordpress theme file, i simple want user can login with email id not using username please help me about this – user3811650 Oct 6 '15 at 7:03
  • 2
    Yes, we understand that you added this code in functions.php file but then what is the problem? What is your question? Are you getting any error? Explain your issue. – Robert hue Oct 6 '15 at 7:06
  • When i add code remove_filter('authenticate', 'wp_authenticate_username_password', 20); with below code then i got error message,message is "Invalid username or password. Please try again!" remove_filter('authenticate', 'wp_authenticate_username_password', 20); function login_with_email_address($username) { $user = get_user_by_email($username); if(!empty($user->user_login)) $username = $user->user_login; return $username; } add_action('wp_authenticate','login_with_email_address'); – user3811650 Oct 6 '15 at 7:15
  • If you have another hook or concept please give me solution for login using email id not username – user3811650 Oct 6 '15 at 7:33
1

Your code has some problems:

  • You are using deprecated function get_user_by_email
  • You are using wrong hook wp_authenticate

You can this plugin https://wordpress.org/plugins/wp-email-login/

or

Your can use this code

function wpsc_authenticate_user_by_email( $user, $username, $password ) {


    // Bailout
    if( ! is_frontend_login_form() ){
       return $user;
    }

    $login_page  = strtok( $_SERVER["HTTP_REFERER"], '?' );

    //check for valid inputs
    if( $username == "" || $password == "" ) {
        wp_redirect( $login_page . "?login=empty" );
        exit;
    }

    // get user by email
    if ( is_email( $username ) ) {
        $user =  get_user_by( 'email', $username );

        // validate user
        if ( $user && wp_check_password( $password, $user->user_pass, $user->ID ) ) {
            return $user;
        }else{
            wp_redirect( $login_page . '?login=failed' );
            exit;
        }
    }else{
        wp_redirect( $login_page . '?login=failed' );
        exit;
    }


    return $user;
}

add_filter( 'authenticate', 'wpsc_authenticate_user_by_email', 1, 3 );

Define conditions in is_frontend_login_form() to check if user is coming from your frontend form or not.

  • Thanks @Ravs for giving url and codes but when i add above given code in my theme file then website not running my be i added in wroung way can you explain me how to add code and where we have to add above code and also where we have to apply is_frontend_login_form( – user3811650 Oct 6 '15 at 8:40

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.