Here is the code I'm using

First i have Removed Wordpress Authentication

remove_filter('authenticate', 'wp_authenticate_username_password', 20);

Then i have added my own authentication

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

    $phone = $wpdb->escape($_REQUEST['phone']);
    $password   = $wpdb->escape($_REQUEST['password']);

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

            $error->add('empty_phone', __('<strong>ERROR</strong>: Phone field is empty.'));
            $error->add('empty_password', __('<strong>ERROR</strong>: Password field is empty.'));

        return $error;

    $user = reset(
                   'meta_key' => 'phone',
                   'meta_value' => $phone,
                   'number' => 1,
                   'count_total' => false

        $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;
            return $user; //passed
}, 20, 3);

And Finally my HTML Form

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

it seems not working. i'm entering the right phone number and password but all i have is page reloading. any help or comment?

  • Do you have debugging enabled? If so, do you get any errors? – kero May 12 '18 at 12:17
  • [12-May-2018 15:41:23 UTC] PHP Fatal error: Uncaught Error: Call to a member function escape() on null in C:\xampp\htdocs\site\wp-content\themes\twentyseventeen\functions.php:779 – abdelrhman kouta May 12 '18 at 15:43
  • the problem with $user_phone = $wpdb->escape($_REQUEST['user_phone']); – abdelrhman kouta May 12 '18 at 15:44

Wordpress "login" process assumes that a user name is given. If there is no user name, there is nothing to authenticate and most likely your code is not being reached at all. If you want to have your own form, just write your own authentication.

The other path you can follow is to change the label of the user field in the "normal" form to "phone" without replacing any other thing in the HTML itself. If you do that all the expected filters and actions should be triggered and your code is more likely to work.


i did it the problem was i was accessing the $wpdb without calling it globally first within the function, thank you very much

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