I want to login with username,email id and phone number in wordpress

10,737

Solution 1

Try this one :

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);

Next, we’re going to add our own authentication. To do so, we’re going to use add_filter.

Add the following code to your functions.php files:

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

    //We shall SQL escape all inputs
    $user_phone = $wpdb->escape($_REQUEST['user_phone']);
    $password   = $wpdb->escape($_REQUEST['password']);

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

            if(empty($user_phone)){ //No email
                $error->add('empty_phone', __('<strong>ERROR</strong>: Phone field is empty.'));
            }
            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);
        //Instead of the above one use this 
        $user = reset(
                     get_users(
                      array(
                       'meta_key' => 'user_phone',
                       'meta_value' => $user_phone,
                       'number' => 1,
                       'count_total' => false
                      )
                     )
                    );

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

Form this time should be :

Ofcourse if you want one input field for all then it can be done. But you need to decide how will you choose which validation to be performed phonenumber , email address or Username

<form id="login" name="form" action="<?php echo home_url(); ?>/login/" method="post">
        <input id="user_phone" type="text" placeholder="Phone Number" name="user_phone">
        <input id="password" type="password" placeholder="Password" name="password">
        <input id="submit" type="submit" name="submit" value="Submit">
</form>

Solution 2

you can use this plugin to login and register with phone number or email and password. the code will be send to you with sms or email and you can authenticate with that. and your customer can set password for him/her , so it does not need to send activation code via sms or email again.

https://wordpress.org/plugins/login-with-phone-number/

Solution 3

This is not the preferred solutions as we need to create a custom login form for achieving this

You need to create a template at front end to be displayed like this :

<form id="login" name="form" action="<?php echo home_url(); ?>/login/" method="post">
        <input id="username" type="text" placeholder="Username" name="username">
        <input id="user_phone" type="text" placeholder="Phone Number" name="user_phone">
        <input id="password" type="password" placeholder="Password" name="password">
        <input id="submit" type="submit" name="submit" value="Submit">
</form>

Above that write this php code which will work on form

I am getting the User ID of the user by its username and then getting User Phone in the user_meta table by User ID and then matching it with submitted user phone number.

You can make changes in the below code for validating the phone number separately.

I have assumed the meta_key and input type name both as "user_phone"

if($_POST) {

    global $wpdb;

    //We shall SQL escape all inputs
    $user_phone = $wpdb->escape($_REQUEST['user_phone']);
    $username   = $wpdb->escape($_REQUEST['username']);
    $password   = $wpdb->escape($_REQUEST['password']);
    $remember   = $wpdb->escape($_REQUEST['rememberme']);

    $user = get_userdatabylogin($username);


    if($remember) $remember = "true";
    else $remember = "false";

    $login_data = array();
    $login_data['user_login'] = $username;
    $login_data['user_password'] = $password;
    $login_data['remember'] = $remember;

    //get user phone number from DB
    $org_phone = get_user_meta($user->ID , 'user_phone',1);

    $user_verify = wp_signon( $login_data, false ); 

    if ( is_wp_error($user_verify) && $org_phone == $user_phone) 
    {
       header("Location: " . home_url() . "/login/error/");
       // Note, I have created a page called "Error" that is a child of the login page to handle errors. This can be anything, but it seemed a good way to me to handle errors.
     } else {    
       echo "<script type='text/javascript'>window.location='". home_url() ."'</script>";
       exit();
     }

} else {

    // No login details entered - you should probably add some more user feedback here, but this does the bare minimum

    echo "Invalid login details";

}
Share:
10,737
Harsh
Author by

Harsh

Working as Software Developer. Good in Wordpress Development, worked in core php and also provide backend management from php side for App Developers. Developed Wordpress Plugin Event Calendar With Map View(ECWMV) https://wordpress.org/plugins/event-calendar-with-map-view/

Updated on June 30, 2022

Comments

  • Harsh
    Harsh about 2 years

    I want to login in wordpress with username , email id and phone number. where I am inserting phone number in User meta at the time of User registration. Is there any hook where I can access user meta and validate it with password for login. https://wordpress.org/plugins/wp-email-login/
    I came to know that this plugin is used for username and email id validation at the login time. So, I can login with username and email id but my main issue was phone nmber which is in user meta table.

  • Harsh
    Harsh over 8 years
    I need hook for WP_login_form which form was calling in the www.example.com/wp-admin or www.example.com/wp-login.php
  • Harsh
    Harsh over 8 years
    One more thing only 2 input fields one for username,emailid, and phone number and other for password
  • Harsh
    Harsh over 8 years
    Buddy I know how to do with this with core php and I can use ajax for this custom query and all but I need this in the wp_login_form. but never mind thanks for your answer
  • Prakash Rao
    Prakash Rao over 8 years
    In wp-includes/user.php you will find a function wp_signon() which takes 2 arguments, under it calls a hook wp_authenticate which again take two parameters as $username and $password. You can override this function (wp_signon) if you want . Not Preferred
  • Prakash Rao
    Prakash Rao over 8 years
    Problem is that you can get user by their email address but not by their meta fields ..