Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 the 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.

Many Thanks,

Robin

share|improve this question
are you using the default register form or a custom one? – Bainternet Feb 9 '11 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. – Robin I Knight Feb 9 '11 at 18:57

3 Answers

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

share|improve this answer
1  
Isn't there a registration filter that can be hooked? – Zack Feb 9 '11 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. – Robin I Knight Feb 9 '11 at 21:00

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. :)

share|improve this answer
1  
user_register is quite a nice place to hook in, for this I think? – jsims281 Sep 2 '11 at 10:28

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' );
share|improve this answer
Should this still send an email to confirm the registration? I am no longer receiving this. – codecowboy Sep 27 '11 at 8:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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