2
I would like to change the Wordpress login form of Username/Password for Email/Password or also Custom Field/Password.
2
I would like to change the Wordpress login form of Username/Password for Email/Password or also Custom Field/Password.
3
Create a file called force-email-login.php
containing the plugin script below and put in the folder wp-content/plugins
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, "", "" );
}
}
}
0
With the function below you can log in with the email or password. Just put the code on 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);
Browser other questions tagged php wordpress
You are not signed in. Login or sign up in order to post.
Do you want to replace username login with email login? What do you mean by custom field?
– fpg1503
You can use the following plugin: https://wordpress.org/plugins/force-email-login/
– Edi. Gomes