带有来自邮件的链接的自动登录用户

时间:2014-05-05 作者:user51254

我创建了一种自动登录WordPress的方式,通过发送来自邮件芯片等提供商的新闻稿。当用户单击此新闻稿中的链接时,他/她应该能够自动登录到自己的帐户,而无需输入登录信息。我已经有了脚本的一部分,但我不知道如何从不得不发送的链接中获取链接。

我找到了this solution 关于StackOverflow,但我不知道如何在我的站点中实现该解决方案。

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

您可以从本教程中获得一些想法:

http://carlofontanos.com/auto-login-to-wordpress-from-another-website/

希望有帮助。

SO网友:Madalin Fintina

你可以试试这个。https://gist.github.com/butlerblog/d3cd7df4ead4736521d1如果链接失效,代码如下:

<?php
/*
   This code snippet can be added to your functions.php file (without the <?php)
   to add a query string to the login link emailed to the user upon registration
   which when clicked will validate the user, log them in, and direct them to 
   the home page.
*/

/**
 * This first function is hooked to the \'user_register\' action which fires
 * during the new user registration process. The process here gets some of
 * the data from the registration process that we will use later in the
 * new user registration email. It also creates and stores an activation
 * key that can be used as an extra security step.
 */
add_action( \'user_register\', \'get_register_data_for_auto_login\' );
function get_register_data_for_auto_login( $user_id ) {

     // Create array that can be picked up later for the email.
    global $my_array;
    $my_array[\'ID\'] = $user_id;

    // Get the username, add it to the array.
    if ( isset(  $_POST[\'user_login\'] ) ) {
        $my_array[\'user_login\'] =  $_POST[\'user_login\'];
    } else {
        $user_info = get_userdata( $user_id );
        $my_array[\'user_login\'] = $user_info->user_login;
    }

    // Create an activation key, add to array.
    $my_array[\'activation_key\'] = md5( microtime() . rand() );
    update_user_meta( $user_id, \'auto_log_key\', $my_array[\'activation_key\'] );
}


/**
 * This is a filter function that filters the email content as it is being
 * handled by wp_mail(). I chose to do this rather than set up a function for
 * the new user registration email because that would need to be done as a 
 * plugin, since that function is a pluggable function. So this method checks
 * the email subject line to see if it is the new registration email. If it is
 * it replaces the original login link with the login link plus a query string
 * containing the user\'s ID, username, and activation key (from the previous
 * function above).
 */
add_filter( \'wp_mail\', \'set_up_auto_login_link\' );
function set_up_auto_login_link( $atts ) {

    // Check if email subject contains "Your username and password".
    if ( isset ( $atts [\'subject\'] ) && $atts[\'subject\'] = \'Your username and password\' ) {
        if ( isset( $atts[\'message\'] ) ) {

            // Pick up the global array of user info from \'user_register\' action.
            global $my_array;

            // Assemble the data for the query string.
            $qstr = \'?id=\' . $my_array[\'ID\'] . \'&u=\' . $my_array[\'user_login\'] . \'&k=\' . $my_array[\'activation_key\'];

            // Prepare data for search/replace on the login link (to add the query string).
            $old = \'/wp-login.php\';
            $new = \'/wp-login.php\' . $qstr;

            // Replace the original login link with the new one containing query string data for auto login.
            $atts[\'message\'] = str_replace( $old, $new, $atts[\'message\'] );
        }
    }

    return $atts;
}


/**
 * This action is hooked to \'init\' which fires right away. The attached function
 * will look to see if the query string parameters we created for doing auto login
 * are there and if they are, it will validate them. To keep this process from 
 * being a giant security hole and allowing anyone with a user ID to become logged
 * in, all three elements must be present and must match the user account:
 * User ID, username, and the activation key.
 * 
 * @todo We could look for a better option that only fires on the wp-login.php.
 */
add_action( \'init\', \'auto_log_user_in\' );
function auto_log_user_in() {

    // If ID, user, and key are all present.
    if ( isset( $_GET[\'id\'] ) 
      && isset( $_GET[\'u\'] ) 
      && isset( $_GET[\'k\'] ) ) {

        // Get the query string values.
        $user_id    = $_GET[\'id\'];
        $user_login = $_GET[\'u\'];
        $activation = $_GET[\'k\'];

        // Get the user data for validation.
        $chk_user = get_user_by( \'id\', $user_id );

        // If a user is returned and it\'s not an admin, validate and login.
        if ( $chk_user && ! user_can( $chk_user->ID, \'manage_options\' ) ) {

            if ( $chk_user->user_login == $user_login && $chk_user->auto_log_key == $activation ) {

                wp_set_current_user( $chk_user->ID, $user_login );
                wp_set_auth_cookie( $chk_user->ID );
                do_action( \'wp_login\', $user_login );
                wp_redirect( home_url() );
                exit();

            }
        }
    }
}

SO网友:keepkalm

我发现有几个插件可以做到这一点。

Byepass

Passwordless Login

SO网友:Omid Ahmadyani
    //get user\'s ID
    $user = get_user_by(\'login\', $loginusername);
    $user_id = $user->ID;
    //login
    wp_set_current_user($user_id, $loginusername);
    wp_set_auth_cookie($user_id);
    do_action(\'wp_login\', $loginusername);
    //redirect to home page after logging in (i.e. don\'t show content of www.site.com/?p=1234 )
    wp_redirect( home_url() );
结束