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();
if(empty($phone)){
$error->add('empty_phone', __('<strong>ERROR</strong>: Phone field is empty.'));
}
if(empty($password)){
$error->add('empty_password', __('<strong>ERROR</strong>: Password field is empty.'));
}
return $error;
}
$user = reset(
get_users(
array(
'meta_key' => 'phone',
'meta_value' => $phone,
'number' => 1,
'count_total' => false
)
)
);
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);
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">
</form>
it seems not working. i’m entering the right phone number and password but all i have is page reloading. any help or comment?
2 Answers
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.