让WordPress支持用户名或邮箱登录

允许你的WordPress站点通过用户名或邮箱登录,是提高用户体验的好方法,毕竟很多时候,用户不太记得自己注册的用户名。同时我们还要将默认的“用户名”修改为“用户名/邮箱”提示用户:

login-with-username-or-email-address-wpdaxue_com

将下面的代码添加到当前主题的 functions.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
//让WordPress支持用户名或邮箱登录
function dr_email_login_authenticate( $user, $username, $password ) {
	if ( is_a( $user, 'WP_User' ) )
		return $user;
 
	if ( !empty( $username ) ) {
		$username = str_replace( '&', '&', stripslashes( $username ) );
		$user = get_user_by( 'email', $username );
		if ( isset( $user, $user->user_login, $user->user_status ) && 0 == (int) $user->user_status )
			$username = $user->user_login;
	}
 
	return wp_authenticate_username_password( null, $username, $password );
}
remove_filter( 'authenticate', 'wp_authenticate_username_password', 20, 3 );
add_filter( 'authenticate', 'dr_email_login_authenticate', 20, 3 );
 
//替换“用户名”为“用户名 / 邮箱”
function username_or_email_login() {
	if ( 'wp-login.php' != basename( $_SERVER['SCRIPT_NAME'] ) )
		return;
 
	?><script type="text/javascript">
	// Form Label
	if ( document.getElementById('loginform') )
		document.getElementById('loginform').childNodes[1].childNodes[1].childNodes[0].nodeValue = '<?php echo esc_js( __( '用户名/邮箱', 'email-login' ) ); ?>';
 
	// Error Messages
	if ( document.getElementById('login_error') )
		document.getElementById('login_error').innerHTML = document.getElementById('login_error').innerHTML.replace( '<?php echo esc_js( __( '用户名' ) ); ?>', '<?php echo esc_js( __( '用户名/邮箱' , 'email-login' ) ); ?>' );
	</script><?php
}
add_action( 'login_form', 'username_or_email_login' );

//让WordPress支持用户名或邮箱登录
function dr_email_login_authenticate( $user, $username, $password ) {
if ( is_a( $user, ‘WP_User’ ) )
return $user; if ( !empty( $username ) ) {
$username = str_replace( ‘&’, ‘&’, stripslashes( $username ) );
$user = get_user_by( ’email’, $username );
if ( isset( $user, $user->user_login, $user->user_status ) && 0 == (int) $user->user_status )
$username = $user->user_login;
} return wp_authenticate_username_password( null, $username, $password );
}
remove_filter( ‘authenticate’, ‘wp_authenticate_username_password’, 20, 3 );
add_filter( ‘authenticate’, ‘dr_email_login_authenticate’, 20, 3 ); //替换“用户名”为“用户名 / 邮箱”
function username_or_email_login() {
if ( ‘wp-login.php’ != basename( $_SERVER[‘SCRIPT_NAME’] ) )
return; ?><script type="text/javascript">
// Form Label
if ( document.getElementById(‘loginform’) )
document.getElementById(‘loginform’).childNodes[1].childNodes[1].childNodes[0].nodeValue = ‘<?php echo esc_js( __( ‘用户名/邮箱’, ’email-login’ ) ); ?>’; // Error Messages
if ( document.getElementById(‘login_error’) )
document.getElementById(‘login_error’).innerHTML = document.getElementById(‘login_error’).innerHTML.replace( ‘<?php echo esc_js( __( ‘用户名’ ) ); ?>’, ‘<?php echo esc_js( __( ‘用户名/邮箱’ , ’email-login’ ) ); ?>’ );
</script><?php
}
add_action( ‘login_form’, ‘username_or_email_login’ );

如果你不喜欢折腾代码,可以下载安装 WP Email Login 插件。

本人擅长Ai、Fw、Fl、Br、Ae、Pr、Id、Ps等软件的安装与卸载,精通CSS、JavaScript、PHP、ASP、C、C++、C#、Java、Ruby、Perl、Lisp、Python、Objective-C、ActionScript、Pascal等单词的拼写,熟悉Windows、Linux、OS X、Android、iOS、WP8等系统的开关机。

通过下面的方式来联系我们:

电邮:138762189@qq.com

联系QQ:点击这里给我发消息

官方站:www.tadke.com

※ ※ 联系请加我的企鹅号 ※※

※ ※技术支持请微信联系站长 ※※

Copyright © 2023 Tadke.com. 琼ICP备20000547号