wordpress怎么设置为邮箱登录
WordPress注册账号都是英文字母组成。时间一久,可能很多用户都会忘记注册账号及密码。一个用户名就成为了绊脚石。
但是我们如果换成邮箱登录就不一样了。很多用户就只有一两个邮箱账号,就很容易记起用户名及密码。
我们甚至可以为了安全,禁止使用用户名登录,而只使用email登录。如何实现呢?
在主题functions.php文件中,增加下面的代码:
// 修改WordPress用户名过滤机制,通过Email获取用户名 function utubon_allow_email_login($username, $raw_username, $strict) { ? if (filter_var($raw_username, FILTER_VALIDATE_EMAIL)) { ??? $user_data = get_user_by('email', $raw_username); ??? if (empty($user_data)) ????? wp_die(__('<strong>ERROR</strong>: There is no user registered with that email address.'), '用户名不正确'); ??? else ????? return $user_data->user_login; ? }else { ??? return $username; ? } } // 修改登录界面的文字,"用户名"改成"用户名或邮箱" function utubon_change_text() { ? echo '<script type="text/javascript"> ??????????????? var user_login_node = document.getElementById("user_login"); ??????????????? var old_username_text = user_login_node.parentNode.innerHTML; ??????????????? user_login_node.parentNode.innerHTML = old_username_text.replace(/用户名/, "用户名或邮箱"); ????? </script>'; } if (in_array($GLOBALS['pagenow'], array('wp-login.php')) && strpos($_SERVER['REQUEST_URI'], '?action=register') === FALSE && strpos($_SERVER['REQUEST_URI'], '?action=lostpassword') === FALSE && strpos($_SERVER['REQUEST_URI'], '?action=rp') === FALSE ) { ? add_filter('sanitize_user', 'utubon_allow_email_login', 10, 3); ? add_action('login_footer', 'utubon_change_text'); }
如果要实现只能用邮箱登录,提供了下面的代码
if (in_array($GLOBALS['pagenow'], array('wp-login.php')) && strpos($_SERVER['REQUEST_URI'], '?action=register') === FALSE && strpos($_SERVER['REQUEST_URI'], '?action=lostpassword') === FALSE && strpos($_SERVER['REQUEST_URI'], '?action=rp') === FALSE ) { ?? ?// remove the default filter ?? ?remove_filter( 'authenticate', 'wp_authenticate_username_password', 20, 3 ); ?? ?// add custom filter ?? ?add_filter( 'authenticate', 'my_authenticate_username_password', 20, 3 ); ?? ?// 修改登录框提示为使用邮箱登录 ?? ?add_action('login_footer', 'change_login_text'); } // Change login credentials function my_authenticate_username_password( $user, $username, $password ) { ??? // If an email address is entered in the username box, ??? // then look up the matching username and authenticate as per normal, using that. ??? if ( ! empty( $username ) ) { ??????? //if the username is not email set username to blank string ??????? //causes authenticate to fail ?? ??? ?if(!filter_var($username,FILTER_VALIDATE_EMAIL)){ ??????????????? $username = time(); ??????????? } ??????? $user = get_user_by( 'email', $username ); ??????? } ??? if ( isset( $user->user_login, $user ) ) ??????? $username = $user->user_login; ??? // using the username found when looking up via email ??? return wp_authenticate_username_password( NULL, $username, $password ); } // 修改登录界面的文字,"用户名"改成"用户名或邮箱" function change_login_text() { ? echo '<script type="text/javascript"> ??????????????? var user_login_node = document.getElementById("user_login"); ??????????????? var old_username_text = user_login_node.parentNode.innerHTML; ??????????????? user_login_node.parentNode.innerHTML = old_username_text.replace(/用户名/, "用户邮箱"); ????? </script>'; }
0条评论