-
Notifications
You must be signed in to change notification settings - Fork 4
/
force-email-login.php
executable file
·50 lines (42 loc) · 1.21 KB
/
force-email-login.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?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.6.0
Author URI: https://miya.io/
*/
$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, "", "" );
}
}
}