2

I created (a sort of) automatic login to WordPress by sending newsletters from providers like Mail Chip. When the user clicks on a link in this newsletter, s/he should be able to automatically log into her/his account without having to enter the login information. I already have a portion of the script, but I do not know how to get links from having to send.

I have found this solution on StackOverflow, but I do not understand how to implement the solution in my site.

  • ALL CAPS headline? Please stay back from SHOUTING as this is how all caps text is interpreted in the web. Thanks. Aside from that: I can't see how your links are formatted and you haven't shown us how you "tried" to implement the solution from SO. Please file an edit and add that information. – kaiser May 5 '14 at 15:21
  • Please do not shout. Improper use of bold lettering is considered as shouting, that is not allowed – Pieter Goosen May 5 '14 at 15:22
  • Sorry guys, but i are new user from there. – user51254 May 5 '14 at 15:30
  • I insert this code in my fuction.php but i not understand how this working. I do not know PHP and have to inzi with WP – user51254 May 5 '14 at 15:32
  • @user51254 you need to be able to understand PHP to understand PHP code, unless you're wanting something you can blindly copy paste I don't see how your question can be answered effectively. We can explain the process in plain english but that would leave you no better off without beginner level PHP skills – Tom J Nowell Apr 7 '17 at 10:23
-3

You can grab some ideas from this tutorial:

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

hope it helps.

  • 5
    Please add at least a small summary of what this tutorial explains. If this link dies the info dies with it. – kraftner Jul 3 '14 at 11:32
1

You can try this. https://gist.github.com/butlerblog/d3cd7df4ead4736521d1 in case the link dies here is the code:

<?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();

            }
        }
    }
}
0

There are a couple of plugins that I found that will do this.

Byepass

Passwordless Login

0
    //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() );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.