如何仅使用没有用户名的电子邮件登录?


20

经过几天的搜索并阅读了2年的旧线程,我很难找到解决方案,使用户仅通过电子邮件登录。

起初,我很高兴看到WP_Email_Login只是发现您仍然可以使用用户名登录。我不确定如何将其编写为插件。我的想法是重写register_new_user函数。我没有在“可插拔”功能列表中看到此信息。我可以使用过滤器/操作来做到这一点吗?

我意识到编辑核心文件并不时尚,因此我希望可以找到一种解决方案,但是如果不存在这种解决方案,我会抓住机会。在wp-login.php中“ register_new_user”函​​数的第一行中,我可以添加:

$nickname_variable(??) = $user_login // set the nickname to the username
$user_login = $user_email; // set the user_login/username to the email address

由于WordPress不允许人们更改用户名,因此效果很好。在注册屏幕(窗体)中,要求输入用户名和电子邮件;我想将Username设置为Nickname变量(如果有人可以告诉我昵称变量是什么,或者在注册过程中在哪里设置的变量,我们将不胜感激)。

干杯,

史密斯


您是否要完全放弃用户名?为什么“电子邮件登录”插件对您不起作用?
瑞安

我真的很好奇您为什么要删除用户名,因为这些是WordPress中所有用户信息的基础。这有点像试图取消Posts的工作-付出大量的工作却没有很多回报,并且保证了以后的问题。
SickHippie 2012年

1
@ Ryan-我认为我无法摆脱用户名,因此我只是强迫用户名等于电子邮件地址。
agentsmith666

@SickHippie-对于登录,我认为电子邮件比用户名更好,更独特。用户发布时,我宁愿使用用户名作为昵称。您是对的,要“摆脱”变量“用户名”会很痛苦,这就是为什么我没有。我只是在注册时为用户选择用户名(用户名将是他们的电子邮件地址;昵称将是他们作为用户名输入的名称)。最终,所有变量都完好无损。
agentsmith666

1
@SickHippie-您是对的,因为Wordpress的默认设置是不允许用户更改其用户名,即使他们要更改其联系电子邮件地址,其用户名/电子邮件地址也将保持不变。我认为从一开始就以我个人的经验,我发现人们很少“删除”电子邮件地址。他们可能会获得新的,但通常他们仍然会拥有旧的。如果不是在极少数情况下,我们将在数据库中手动更改它。非常感谢您的反馈和见解SickHippie!现在,我希望为我的帖子提供解决方案:)
agentsmith666 2012年

Answers:


19

更新: 我已经创建了一个用于登录,注册和通过电子邮件检索密码的插件。https://wordpress.org/plugins/smart-wp-login/

简而言之,您可以将WordPress配置为使用电子邮件登录。

三个步骤:

  • 删除默认身份验证功能
  • 添加自定义身份验证功能
  • 将wp-login.php中的文本“用户名”更改为“电子邮件”

一注:

  • 不要编辑核心文件。

删除WordPress默认身份验证功能。

WordPress使用“ authenticate ”过滤器在用户登录时执行其他验证。

remove_filter('authenticate', 'wp_authenticate_username_password', 20);

添加自定义身份验证功能

add_filter('authenticate', function($user, $email, $password){

    //Check for empty fields
    if(empty($email) || empty ($password)){        
        //create new error object and add errors to it.
        $error = new WP_Error();

        if(empty($email)){ //No email
            $error->add('empty_username', __('<strong>ERROR</strong>: Email field is empty.'));
        }
        else if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ //Invalid Email
            $error->add('invalid_username', __('<strong>ERROR</strong>: Email is invalid.'));
        }

        if(empty($password)){ //No password
            $error->add('empty_password', __('<strong>ERROR</strong>: Password field is empty.'));
        }

        return $error;
    }

    //Check if user exists in WordPress database
    $user = get_user_by('email', $email);

    //bad email
    if(!$user){
        $error = new WP_Error();
        $error->add('invalid', __('<strong>ERROR</strong>: Either the email or password you entered is invalid.'));
        return $error;
    }
    else{ //check password
        if(!wp_check_password($password, $user->user_pass, $user->ID)){ //bad password
            $error = new WP_Error();
            $error->add('invalid', __('<strong>ERROR</strong>: Either the email or password you entered is invalid.'));
            return $error;
        }else{
            return $user; //passed
        }
    }
}, 20, 3);

将wp-login.php中的文本“用户名”更改为“电子邮件”

我们可以使用gettext过滤器将文本“ Username”更改为“ Email”,而无需编辑核心文件。

add_filter('gettext', function($text){
    if(in_array($GLOBALS['pagenow'], array('wp-login.php'))){
        if('Username' == $text){
            return 'Email';
        }
    }
    return $text;
}, 20);

我还在我的博客http://www.thebinary.in/blog/wordpress-login-using-email/中撰写了详细的文章


2
不错的答案Nishant
Andrew Bartel

有用!就我而言,我只是剥离电子邮件的特殊字符并使其成为用户名。因此user@example.com成为user_example_com,并且可以正常工作。
wpcoder

6

有可能,您必须更改名称的过滤器。

// remove the default filter
remove_filter( 'authenticate', 'wp_authenticate_username_password', 20, 3 );
// add custom filter
add_filter( 'authenticate', 'fb_authenticate_username_password', 20, 3 );
function fb_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 ) )
        $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 );
}

另一种方法是插件,您可以通过Google oder在插件存储库中找到它;也许这个插件


感谢您的答复,但是我不确定您是否阅读了我的帖子或者我不太清楚。我为后者道歉。在我的原始帖子中,我提到了WP_Email_Login插件;您的代码和链接来自的确切插件。这是我的原始帖子:“起初,我很高兴看到WP_Email_Login只是发现您仍然可以使用用户名登录。” <---看到问题,我仍然可以使用用户名,因此这就是该插件无法使用的原因。由于无法删除用户名,因此我考虑通过强制用户名等于电子邮件地址来覆盖注册功能。
agentsmith666 2012年

但是,我正在寻找一种无需编辑核心文件即可完成此操作的方法。如果不可能的话,那很好,但是我想知道两种方式。谢谢!
agentsmith666'5

这是一种无需编辑核心文件的方法。将代码复制到插件中,激活并准备就绪。
bueltge

4

使用上面的代码:

// Change login credentials
// 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 );
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 doesn't contain a @ set username to blank string
        //causes authenticate to fail
        if(strpos($username, '@') == FALSE){
                $username = '';
            }
        $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 );
} 

我们要做的就是检查提供的用户名至少是否像电子邮件,如果不破坏用户名。


'@'Wordpress具有方便的内置函数,而不是用户名中的原始字符串检查:sanitize_email将返回有效的电子邮件地址格式或不返回任何内容:sanitize_email('email¬!"@business_com'); // Returns nothing
indextwo 2014年

3

它已经在WP-CORE

现在,wordpress已经允许将EMAIL注册为用户名。但是,如果您谈论的是已经注册的用户,请尝试列出的答案。


1

创建优雅的解决方案只需要对上面的代码进行少量修改即可。Authenticate挂钩文档指出应返回一个WP_User或多个WP_Error对象。

wp_authenticate_username_password函数源代码通过一些非常简单的检查来运行。我们可以复制完成这些检查的方式,并创建一个新WP_Error对象来处理电子邮件地址。另外,我们甚至可以根据需要调整wp_authenticate_username_password代码并对其进行修改,尽管除非您真的想自定义事物的功能,否则这似乎是不必要的。下面的代码应该可以解决这个问题:(尽管我自己还没有测试过……)

// Remove the default authentication function
remove_filter( 'authenticate', 'wp_authenticate_username_password', 20, 3 );

// Add the custom authentication function
add_filter( 'authenticate', 'custom_authenticate_username_password', 20, 3 );

function custom_authenticate_username_password( $user, $username, $password ) {

    // Get the WP_User object based on the email address
    if ( ! empty( $username ) ) {
        $user = get_user_by( 'email', $username );
    }

    // Return a customized WP_Error object if a WP_User object was not be returned (i.e. The email doesn't exist or a regular username was provided)
    if ( ! $user ) {
        return new WP_Error( 'invalid_username_email', sprintf( __( '<strong>ERROR</strong>: Invalid username. Please log in with your email address. <a href="%s" title="Password Lost and Found">Lost your password</a>?' ), wp_lostpassword_url() ) );
    }

    // Hand authentication back over to the default handler now that we a have a valid WP_User object based on the email address
    return wp_authenticate_username_password( null, $username, $password );
}

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.