Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antipole2/829ff97d7f4a69dd87e1270c3831d33a to your computer and use it in GitHub Desktop.
Save antipole2/829ff97d7f4a69dd87e1270c3831d33a to your computer and use it in GitHub Desktop.
WordPress - login by email address only
/* ------------------------------------------------------------------------- *
* (4) 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);
/* ------------------------------------------------------------------------- *
* Remove "Username" from prompt in wp-login.php
/* ------------------------------------------------------------------------- */
function change_login_function() {
add_filter( 'gettext', 'username_change', 20, 3 );
function username_change( $translated_text, $text, $domain )
{
if ($text === 'Username or Email Address')
{
$translated_text = 'Email Address';
}
return $translated_text;
}
}
add_action( 'login_head', 'change_login_function' );
// Fix lost password prompt and add additional advice to the Lost Password page
function ovni_embellish_retrieve_password_message($message){
if (($action = $_REQUEST['action' ]) == "lostpassword"){
$message = str_replace("username or ", "", $message); // drop the reference to username
$message_addition = " If you don't see the email, check your spam filter.<br>If you repeat your request for a reset link, note the time when you make the request. Use the link in the email sent at that time. The link in earlier emails will no longer be valid.</p>";
$message = substr_replace($message,$message_addition,-4);
}
return $message;
}
add_filter('login_message', 'ovni_embellish_retrieve_password_message', 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment