I would like to change the login form of WordPress from Username / Password to Email / Password or also Custom Field / Password.
I would like to change the login form of WordPress from Username / Password to Email / Password or also Custom Field / Password.
Create a file named force-email-login.php
containing the plugin script below and put it in the wp-content/plugins
folder. This will force you to log in with the email (tested by me in version 4.1):
<?php
/*
Plugin Name: Force Email Login
Author: Takayuki Miyauchi
Plugin URI: https://github.com/miya0001/force-email-login
Description: Use email address for login to your WordPress.
Version: 0.5.0
Author URI: https://github.com/miya0001/
*/
$force_email_auth = new Force_Email_Auth();
$force_email_auth->register();
class Force_Email_Auth {
function register()
{
add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
}
public function plugins_loaded()
{
remove_filter( 'authenticate', 'wp_authenticate_username_password', 20, 3 );
add_filter( 'authenticate', array( $this, 'authenticate'), 20, 3 );
}
public function authenticate( $user, $username, $password )
{
if ( is_a( $user, 'WP_User' ) ) {
return $user;
}
if ( ! empty( $username ) && is_email( $username ) ) {
$user = get_user_by( 'email', $username );
if ( isset( $user, $user->user_login, $user->user_status ) ) {
if ( 0 === intval( $user->user_status ) ) {
$username = $user->user_login;
return wp_authenticate_username_password( null, $username, $password );
}
}
}
if ( ! empty( $username ) || ! empty( $password ) ) {
return false;
} else {
return wp_authenticate_username_password( null, "", "" );
}
}
}
Source: link
With the function below you can log in with the email or the password. Just put the code in functions.php
//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))? : get_user_by('login', $email);
//bad email
if (!$user) {
$error = new WP_Error();
$error->add('invalid', __('<strong>ERROR</strong>: O email ou o password é inválido.'));
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);