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

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

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

将下面的代码添加到当前主题的 functions.php 文件即可

//让WordPress支持用户名或<a href="https://www.wpdaxue.com/tag/%e9%82%ae%e7%ae%b1%e7%99%bb%e5%bd%95" title="查看与【邮箱登录】相关的文章" target="_blank" rel="noopener">邮箱登录</a>
function dr_email_login_authenticate( $user, $username, $password ) {
	if ( is_a( $user, 'WP_User' ) )
		return $user;
 
	if ( !empty( $username ) ) {
		$username = str_replace( '&amp;', '&amp;', stripslashes( $username ) );
		$user = get_user_by( 'email', $username );
		if ( isset( $user, $user-&gt;user_login, $user-&gt;user_status ) &amp;&amp; 0 == (int) $user-&gt;user_status )
			$username = $user-&gt;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;
 
	?&gt;&lt;script type=&quot;text/javascript&quot;&gt;
	// Form Label
	if ( document.getElementById('loginform') )
		document.getElementById('loginform').childNodes[1].childNodes[1].childNodes[0].nodeValue = '&lt;?php echo esc_js( __( '用户名/邮箱', 'email-login' ) ); ?&gt;';
 
	// Error Messages
	if ( document.getElementById('login_error') )
		document.getElementById('login_error').innerHTML = document.getElementById('login_error').innerHTML.replace( '&lt;?php echo esc_js( __( '用户名' ) ); ?&gt;', '&lt;?php echo esc_js( __( '用户名/邮箱' , 'email-login' ) ); ?&gt;' );
	&lt;/script&gt;&lt;?php
}
add_action( 'login_form', 'username_or_email_login' );

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

来源:

https://www.wpdaxue.com/login-with-username-or-email-address.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_33206.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?