wordpress用户名允许使用邮箱登陆方法
wordpress中默认中能是英文用户名登录了,以前有介绍过修改它可以中文用户名,下面我再来介绍几种修改可以是邮箱登陆的例子,都是在functions.php函数中处理.
在functions.php里加入下面的代码:
- function email_address_login($username) {
- $user = get_user_by_email($username);
- if(!emptyempty($user->user_login))
- $username = $user->user_login;
- return $username;
- }
- add_action('wp_authenticate','email_address_login');
这样就大功告成了,但是由于wp登陆页面中的提示文本是输入用户名,所以同时你总得让你网站的注册用户知道你现在支持邮箱登陆了吧,打开'wp-login.php',搜索代码:
- <p>
- <label for="user_login"><?php _e('用户名') ?><br />
- <input type="text" name="log" id="user_login" class="input" value="<?php echo esc_attr($user_login); ?>" size="20" tabindex="10" /></label>
- </p>
-
- <p>
- <label for="user_login"><?php _e('用户名或邮箱地址') ?><br />
- <input type="text" name="log" id="user_login" class="input" value="<?php echo esc_attr($user_login); ?>" size="20" tabindex="10" /></label>
- </p>
大功告成.只是唯一的缺点就是你一旦升级就会覆盖修改后的'wp-login.php'文件.上面的做法其实可以用一段代码来代替,代码如下:
-
- function ludou_allow_email_login($username, $raw_username, $strict) {
- if (filter_var($raw_username, FILTER_VALIDATE_EMAIL)) {
- $user_data = get_user_by('email', $raw_username);
-
- if (emptyempty($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 ludou_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', 'ludou_allow_email_login', 10, 3);
- add_action('login_footer', 'ludou_change_text');
- }
同样,添加到functions.php内就可以了.
另一个方法:将下面的代码添加到当前主题的 functions.php 文件即可:
-
- function dr_email_login_authenticate( $user, $username, $password ) {
- if ( is_a( $user, 'WP_User' ) )
- return $user;
-
- if ( !emptyempty( $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">
-
- if ( document.getElementById('loginform') )
- document.getElementById('loginform').childNodes[1].childNodes[1].childNodes[0].nodeValue = '<?php echo esc_js( __( '用户名/邮箱', 'email-login' ) ); ?>';
-
-
- 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' );