Skip to content

Instantly share code, notes, and snippets.

@bestwebsite
Created January 18, 2015 05:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bestwebsite/e410de289a143a288f5a to your computer and use it in GitHub Desktop.
Save bestwebsite/e410de289a143a288f5a to your computer and use it in GitHub Desktop.
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-&gt;add('empty_username', __('<strong>ERROR</strong>: Email field is empty.'));
}
else if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ //Invalid Email
$error-&gt;add('invalid_username', __('<strong>ERROR</strong>: Email is invalid.'));
}
if(empty($password)){ //No password
$error-&gt;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-&gt;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-&gt;user_pass, $user-&gt;ID)){ //bad password
$error = new WP_Error();
$error-&gt;add('invalid', __('<strong>ERROR</strong>: Either the email or password you entered is invalid.'));
return $error;
}else{
return $user; //passed
}
}
}, 20, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment