1

Whenever we login to wpadmin there are two available credentials

  1. username or email
  2. password

I want phone number in place of username so I can login to wpadmin with

  1. Phone Number or Email
  2. password

Is there any possibility to override WP credentials ?

Thanks

3
  • Where are you storing the admin user phone numbers? I don't think that's a standard field. Are you using phone-based 2FA like Duo Security?
    – Rup
    Commented Jul 26, 2016 at 11:30
  • Essentially you want to make a copy of the wp_authenticate_email_password function in wp-includes/user.php, which is more or less Pullata's answer, but that looks up the user by phone number instead, and then register it as an additional 'authenticate' filter. How exactly you do that depends on how you're storing the phone number in the first place. You probably want to normalize the phone number first e.g. using libphonenumber-for-php, provided you stored it that way too.
    – Rup
    Commented Jul 26, 2016 at 11:58
  • for test purpose i am storing phone number in an additional field which i created in wp_user database
    – moiz45
    Commented Jul 26, 2016 at 12:52

2 Answers 2

1

The first thing we need to do is remove the default authentication rights. Add the following snippet to your functions.php file:

//remove wordpress authentication
remove_filter('authenticate', 'wp_authenticate_username_password', 20);

Add the following code to your functions.php files:

add_filter('authenticate', function($user, $email, $password){

//Check for empty fields
    if(empty($email) || empty ($password)){        
        //create new error object and add errors to it.
        $error = new WP_Error();

        if(empty($email)){ //No email
            $error->add('empty_username', __('<strong>ERROR</strong>: Email field is empty.'));
        }
        else if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ //Invalid Email
            $error->add('invalid_username', __('<strong>ERROR</strong>: Email is invalid.'));
        }

        if(empty($password)){ //No password
            $error->add('empty_password', __('<strong>ERROR</strong>: Password field is empty.'));
        }

        return $error;
    }

    //Check if user exists in WordPress database
    $user = get_user_by('email', $email);

    //bad email
    if(!$user){
        $error = new WP_Error();
        $error->add('invalid', __('<strong>ERROR</strong>: Either the email or password you entered is invalid.'));
        return $error;
    }
    else{ //check password
        if(!wp_check_password($password, $user->user_pass, $user->ID)){ //bad password
            $error = new WP_Error();
            $error->add('invalid', __('<strong>ERROR</strong>: Either the email or password you entered is invalid.'));
            return $error;
        }else{
            return $user; //passed
        }
    }
}, 20, 3);

The code checks if the username (now email) or password fields are empty. If neither are empty, it uses get_user_by to look for the user’s email. After finding a valid user, it then checks if the password is correct using the wp_check_password() function.

For further details Click Here

3
  • the above code is for validating purpose where in normal login credentials validating is for user name but here you will get for email
    – Praveen
    Commented Jul 26, 2016 at 11:35
  • thanks @pullata parveen for the answer but my question still remain the same that i want to use phone number and email to login to WP.
    – moiz45
    Commented Jul 26, 2016 at 11:42
  • OK, but WordPress 4.5 supports login with email address out-of-the-box.
    – Rup
    Commented Jul 26, 2016 at 11:42
1

If you would do changes with the core files of wordpress , the changes will be lost on a next upgrade of wordpress.

And if you want to edit the login.php you need to create a custom function which will override the default function (which is using for 'UserName')

You can follow this link

How to login with email only no username?

1
  • thanks @mukii kumar for the answer but i want to log in with phone number and email to WP.
    – moiz45
    Commented Jul 26, 2016 at 11:45

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.