17

I am using a plugin modified for my purposes.

What I am after is after the user has registered for it to automatically log them in and return them to the current page. At the moment it sends them an email with their username and password. They then have to log in using those details.

4
  • are you using the default register form or a custom one?
    – Bainternet
    Feb 9, 2011 at 18:46
  • A custom one but it is based on redirects so codes designed to work with the default system may well work, and if not I can probably modify it to. Feb 9, 2011 at 18:57
  • I think I should point out that automatically logging in a user at registration circumvents part of login security. Ordinarily, a user cannot login without providing a valid email address. The user needs to register, receive an email, then login. If you remove the email step, your users could register with fake addresses, be automatically logged in, and be able to get to the backend, comment, whatever your default subscribers can do. Who would benefit? Spammers, for one. Hackers would also love to poke around for holes in your backend without having to give an address that could be revealing.
    – s_ha_dum
    May 26, 2013 at 23:08
  • A detailed blog: sforsuresh.in/auto-login-wordpress Jan 9, 2018 at 10:44

4 Answers 4

10

Basically to log a user in you can use:

            //Login the user
    $creds = array();
    $creds['user_login'] = $login;
    $creds['user_password'] = $password;
    if ( !empty( $remember ) ){ 
        $creds['remember'] = true;
    }
    $user = wp_signon( $creds, true );

but that is only when you have the password and login so you can create your own register form and process it and create the user yourself

//Only after Everything has been validated, proceed with creating the user
        //Create the user
        $user_pass = wp_generate_password();
        $user = array(
            'user_login' => $username,
            'user_pass' => $user_pass,
            'first_name' => $firstname,
            'last_name' => $lastname,
            'user_email' => $email
        );
        $user_id = wp_insert_user( $user );

        /*Send e-mail to admin and new user - 
        You could create your own e-mail instead of using this function*/
        wp_new_user_notification( $user_id, $user_pass );

and here we have both login and password so you can log the user in.

Hope this helps

2
  • 2
    Isn't there a registration filter that can be hooked?
    – Zack
    Feb 9, 2011 at 20:30
  • 1
    Tricky, Ill see if I can integrate it. There is no easier way then. I take it wordpress has no interest in providing a nice convenient get_the_password() as it emails that out. Feb 9, 2011 at 21:00
5

There's not an ideal place to hook into the registration process. I think there's a strong case to add a user registration event action hook to core. But I think you might be able to fake it, in the mean-time. One of the last things that happens when a user successfully registers, is the creation of a user option named 'default_password_nag'. We can create an action to watch for that, and set the user up when it's set.

add_action('update_user_metadata', 'my_auto_login', 10, 4);

function my_auto_login( $metaid, $userid, $key, $value ) {
    // We only care about the password nag event. Ignore anything else.
    if ( 'default_password_nag' !== $key  && true !== $value) {
        return;
    }

    // Set the current user variables, and give him a cookie. 
    wp_set_current_user( $userid );
    wp_set_auth_cookie( $userid );
}

Untested, but should work, in theory.

Now that we have an idea what to do, I'll opine that I think this is a bad idea, security-wise. People can create junk accounts without even having to go through the trouble of setting up a junk email dropbox. :)

1
  • 1
    user_register is quite a nice place to hook in, for this I think?
    – jsims281
    Sep 2, 2011 at 10:28
2

I've just managed to get that functionality working using the user_register hook and the following code in my functions.php:

// auto log in a user who has just signed up       
function auto_login_new_user( $user_id ) {
  wp_set_auth_cookie( $user_id, false, is_ssl() );
}
add_action( 'user_register', 'auto_login_new_user' );
1
  • Should this still send an email to confirm the registration? I am no longer receiving this.
    – codecowboy
    Sep 27, 2011 at 8:37
0
function login_after_register($userlogin,$userpass){
    $credentials = array( 'user_login' =>  $userlogin, 'user_password' => $userpass, 'remember' => true );

    $secure_cookie = is_ssl();

    $secure_cookie = apply_filters('secure_signon_cookie', $secure_cookie, $credentials);
    add_filter('authenticate', 'wp_authenticate_cookie', 30, 3);

    $user = wp_authenticate($credentials['user_login'], $credentials['user_password']);
    wp_set_auth_cookie($user->ID, $credentials["remember"], $secure_cookie);
    do_action('wp_login', $user->user_login, $user);
}
1
  • 1
    Please provide explanations along with your code.
    – s_ha_dum
    May 26, 2013 at 22:47

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.