使用电子邮件登录(WP模式登录)

时间:2013-08-22 作者:SMacFadyen

我正在尝试使用这个插件来使用用户电子邮件登录,而不是用户名。

插件是http://wordpress.org/plugins/wp-modal-login/

有没有一种很好的方法可以做到这一点,也许是通过一个过滤器让这个插件与电子邮件凭据一起工作?

我已尝试使用此筛选器:

function custom_login() {
  $data = array();        
  $data[\'user_login\']     = sanitize_user( $_REQUEST[\'username\'] );
  $data[\'user_email\']     = sanitize_user( $_REQUEST[\'user_email\'] );
  $data[\'user_password\']  = sanitize_text_field( $_REQUEST[\'password\'] );
  $data[\'rememberme\']     = sanitize_text_field( $_REQUEST[\'rememberme\'] );
  $data[\'user_login\']     = sanitize_user( $_REQUEST[\'user_email\'] );
  $user = wp_signon( $creds, false );
  if ( is_wp_error($user) )
    echo $user->get_error_message();
}
// run it before the headers and cookies are sent
add_action( \'after_setup_theme\', \'custom_login\' );

2 个回复
最合适的回答,由SO网友:Eek 整理而成

我正在使用这个简单的动作,它就像一个符咒

<?php
/** Plugin Name: (#111223) User Login with Mail Address */
    add_action( \'wp_authenticate\', \'wpse111223_login_with_email_address\' );
function wpse111223_login_with_email_address( $username ) {
    $user = get_user_by_email( $username );
    if ( ! empty( $user->user_login ) )
        $username = $user->user_login;

    return $username;
}
它与wp\\u authenticate挂钩,并允许通过电子邮件登录WordPress验证系统,因此它应该可以与任何插件一起使用。我成功地将其用于Login with Ajax 插件。

:)

后期编辑:对于那些不知道将代码片段放在哪里的人,它应该放在你的主题中functions.php 或自定义插件。

SO网友:Jake

我使用此基本方法来启用电子邮件登录,类似于另一个答案,但我认为首先测试更干净:

add_filter( \'authenticate\', \'custom_allow_email_login\', 20, 3);
function custom_allow_email_login( $user, $username, $password )
{
    if ( is_email($username) )
    {
        $user = get_user_by_email( $username );
        if ( $user ) $username = $user->user_login;
    }
    return wp_authenticate_username_password( null, $username, $password );
}
不幸的是,如果您想更改表单上的标签,您必须使用Javascript或使用自己的可自定义表单创建自己的登录页面。

使用Javascript:

add_action( \'login_enqueue_scripts\', \'custom_login_enqueue_scripts\' );
function custom_login_enqueue_scripts()
{
    wp_enqueue_script(\'jquery\');
}
add_action( \'login_form\', \'custom_change_username\' );
function custom_change_username()
{
    echo "\\n" . \'
    <script type="text/javascript">
        jQuery(document).ready(function($) {
            $("label").html(function(index,html){
                return html.replace("Username", "Username or Email");
            });
        });
    </script>\' . "\\n";
}
您可以使用wp_login_form 在您自己的页面上包含登录表单。

结束