After searching for a couple of days and reading 2 year old threads I’m having difficulty finding a solution to the problem of having users login by email only.
At first I was delighted to see WP_Email_Login only to find out you can still use your username to login. I’m not sure how to go about writing this as a plugin. My idea is to override the register_new_user function. I didn’t see this on the “pluggable” functions list. Can I use filters/action do accomplish this?
I realize it’s not fashionable to edit the core files so I’m hoping a solution is out there however if one does not exist I’ll take my chances. In the first line of the “register_new_user” function in wp-login.php I can add:
$nickname_variable(??) = $user_login // set the nickname to the username
$user_login = $user_email; // set the user_login/username to the email address
This works quite nicely since WordPress doesn’t allow people to change their username. In the Register Screen (form) it asks for the Username & Email; I’d like to set the Username to the Nickname variable (if someone can tell me what the nickname variable is called or where it’s set during registration that’d be appreciated).
Cheers,
Smith
Update:
I have created a plugin for login, registration and retrieve password with email. https://wordpress.org/plugins/smart-wp-login/
in short, you can configure WordPress to login with email.
Three Steps:
- Remove default authentication function
- Add custom authentication function
- Change text “Username” in wp-login.php to “Email”
One Note:
Remove WordPress default authentication function.
WordPress uses “authenticate” filter to perform additional validation at user login.
remove_filter('authenticate', 'wp_authenticate_username_password', 20);
Add custom authentication function
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);
Change text “Username” in wp-login.php to “Email”
We can use gettext filter to change text “Username” to “Email” without editing core files.
add_filter('gettext', function($text){
if(in_array($GLOBALS['pagenow'], array('wp-login.php'))){
if('Username' == $text){
return 'Email';
}
}
return $text;
}, 20);
I have also written a detailed article at my blog http://www.thebinary.in/blog/wordpress-login-using-email/