Created
February 20, 2017 12:32
-
-
Save BHWD/9d7f0c226654d2b2f8bb3066e4da22a5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//remove wordpress authentication | |
remove_filter('authenticate', 'wp_authenticate_username_password', 20); | |
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>: The email 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>: The 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