如何仅使用电子邮件登录,不使用用户名?

时间:2012-05-09 作者:agentsmith666

在搜索了几天并阅读了2年前的帖子后,我很难找到一个解决方案,解决用户只能通过电子邮件登录的问题。

起初,我很高兴看到WP\\u Email\\u登录后发现您仍然可以使用您的用户名登录。我不知道如何将其作为插件来编写。我的想法是重写register\\u new\\u user函数。我在“可插拔”函数列表中没有看到这个。我是否可以使用筛选器/操作来完成此操作?

我意识到编辑核心文件并不流行,所以我希望有一个解决方案,但如果不存在,我会抓住机会。在wp login中“register\\u new\\u user”函数的第一行。我可以添加的php:

$nickname_variable(??) = $user_login // set the nickname to the username
$user_login = $user_email; // set the user_login/username to the email address
由于WordPress不允许用户更改用户名,因此这项功能非常有效。在注册屏幕(表单)中,它要求输入用户名和;电子邮件我想将用户名设置为昵称变量(如果有人能告诉我昵称变量叫什么,或者在注册过程中设置在哪里,我将不胜感激)。

干杯

史密斯

7 个回复
SO网友:Nishant Kumar

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

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

三个步骤:

删除默认身份验证功能添加自定义身份验证功能更改wp登录中的文本“用户名”。php至“电子邮件”注意:

不要编辑核心文件

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登录中更改文本“用户名”。php至“电子邮件”

我们可以使用gettext 筛选以将文本“用户名”更改为“电子邮件”,而不编辑核心文件。

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/

SO网友:bueltge

您可以通过操作过滤器更改默认验证。

// 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 );
}
另一种可能是插件this plugin.

SO网友:Vigs

使用上述代码:

// 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 );
} 
我们所要做的就是检查所提供的用户名是否至少看起来像电子邮件,如果没有破坏用户名的话。

SO网友:Andrew Odri

要创建一个优雅的解决方案,只需对上述代码稍加修改即可。这个documentation for the authenticate hook 声明:WP_User 对象或WP_Error 应返回对象。

这个source code for the wp_authenticate_username_password function 通过一些非常简单的检查;我们可以复制这些检查的方式,并创建一个新的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 );
}

SO网友:T.Todua

它已经在WP-CORE!

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

SO网友:Lucas Bustamante
SO网友:user9869932

我一直在努力做到这一点,没有编码,这里提出的许多插件不再受支持。我找到了一个这样做的:https://wordpress.org/plugins/login-customizer/.

它还可以做其他事情,但它仍然很轻。

结束

相关推荐

My login form does not work

我有以下代码为我博客中的每个页面生成登录表单。<form action=\"<?php echo wp_login_url(); ?>\" method=\"post\"> username: <br /> <input name=\"log\" id=\"login_username\" type=\"text\" /> <br /> password: <br /> <input na