admin管理员组

文章数量:1426839

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!"

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!"

Share Improve this question edited Oct 13, 2015 at 13:12 kaiser 50.9k27 gold badges151 silver badges245 bronze badges asked Oct 6, 2015 at 6:36 user3811650user3811650 157 bronze badges 9
  • 3 OK. So you have task and a code. What is missing? What is the problem? – Robert hue Commented Oct 6, 2015 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 Commented Oct 6, 2015 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 Commented Oct 6, 2015 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 Commented Oct 6, 2015 at 7:15
  • If you have another hook or concept please give me solution for login using email id not username – user3811650 Commented Oct 6, 2015 at 7:33
 |  Show 4 more comments

1 Answer 1

Reset to default 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/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.

本文标签: authenticationI want login using email not username wordpress front end