5

I looked up a bunch of threads trying to figure out how to automatically log a user in after registration, and came up with this.

function automatically_log_me_in( $user_id ) {
    $user = get_user_by('id',$user_id);
    $username = $user->user_nicename;
    $user_id = $user->ID;
    wp_set_current_user($user_id, $username);
    wp_set_auth_cookie($user_id);
    do_action('wp_login', $username, $user);
    exit;
}
add_action( 'user_register', 'automatically_log_me_in' );

According to many responses I've seen, wp_set_auth_cookie and wp_set_current_user alone should be enough to log a user in by ID, but even this isn't doing anything.

Does anyone have any idea what I'm doing wrong?

  • 2
    how are you registering users? via wp-login.php? this does successfully log someone in when I test it, though you short-circuit the rest of the script when you exit. – Milo Aug 6 '14 at 23:42
  • 3
    also note that when you set a cookie, it can't be read until the next request, so you need to redirect somewhere for WordPress to recognize that you're logged in. – Milo Aug 6 '14 at 23:44
  • 1
    I'm registering users with a custom registration form that directs to wp-login.php. Could that be it? – Overcode Aug 9 '14 at 2:23
2

Short answer - Your original function (mostly) works. This is your function edited to do what it needs to do:

function automatically_log_me_in( $user_id ) {
    wp_set_current_user( $user_id );
    wp_set_auth_cookie( $user_id );
    wp_redirect( home_url( '/some-ending-page/' ) );
    exit(); 
}
add_action( 'user_register', 'automatically_log_me_in' );

Long answer - here is an explanation of why this is the answer.

The suggestion of wp_signon() won't do it. You need the user credentials for that, and barring passing credentials, that function will attempt to retrieve the posted username ('log') and password ('pwd') from the form and I would assume 'pwd' would be lacking from that.

The other answers make suggestions that unfortunately don't apply in this specific instance. Several are suggestions that are plugin specific. As the developer of one of those (WP-Members), as much as I'd like you to use that plugin, suggesting to use wpmem_post_register_data doesn't do you any good if you're not using WP-Members.

Using wp_set_current_user() as your original thought is the correct way to do it; but you need to redirect the user at the end so they are seen as logged in.

The user_register action passes the ID of the just registered user, so I'm not sure why you would change the value of $user_id by setting it to $user->ID when you already have the value. And the only reason to get the user object in your original function is to get the username. This is an optional argument for wp_set_current_user() and the wp_login action so I wouldn't even bother with that. (I'd ditch the wp_login action because all that is doing is putting a hook into your process for outside functions to hook to. Whether you want/need this requires more information than your OP provides.) All that is just wasted bits in your code, IMO.

Does your registration process end at this point? The auth cookie is set and the user is logged in. However, that won't be read until the next page request so you need to redirect the user to whatever page you want them to end on using wp_redirect() after which you need to exit().

| improve this answer | |
1

I'm registering users with a custom registration form that directs to wp-login.php

Then you could use wp_signon function. Provide more details for suggesting another solution.

| improve this answer | |
1

I'm registering users with a custom registration form that directs to wp-login.php. Could that be it?

Try one of these functions. You can chose which page to redirect to in the first one. The second one is more like the one you were trying. They may work or help.

add_action( 'tml_new_user_registered', 'tml_new_user_registered' );
function tml_new_user_registered( $user_id ) {
    wp_set_auth_cookie( $user_id, false, is_ssl() );
    wp_redirect( admin_url( 'profile.php' ) );
    exit;
}

or

add_action( 'wpmem_post_register_data', 'my_registration_hook', 1 );
function my_registration_hook( $fields ) {
    $user_login = $fields['username'];
    $user_id = $fields['ID'];

    wp_set_current_user( $user_id );
    wp_set_auth_cookie( $user_login );
    do_action( 'wp_login', $user_login );

    wp_set_current_user( $fields['ID'] );
}
| improve this answer | |
  • Neither of these suggestions solve the OP's problem. These are plugin specific - one is for Theme My Login and the other for WP-Members. As one of those plugin's authors, I'd love for you to use my plugin - but that's not the solution to this particular question. – butlerblog Oct 26 '18 at 13:14
0

You need to run this code before any headers are sent to the client. Try segregating the login code.

This question is almost a year old, so please let us know what, if anything worked.

| improve this answer | |
-1

This works for me. I am using WP-Members plugin (and hook).

add_action( 'wpmem_post_register_data', 'my_registration_hook', 1 );
function my_registration_hook( $fields ) {
    wp_set_current_user( $fields['ID'] );
    $creds = array(
        'user_login'    => $fields['username'],
        'user_password' => $fields['password'],
        'rememember'    => false
    );
    wp_signon( $creds, false );
}
| improve this answer | |
  • I think this is not a very good answer for the problem described in the question. – cybmeta Feb 4 '16 at 12:48
  • Why not? WP-Members plugin? – user1075397 Feb 5 '16 at 13:13
  • Because the action wpmem_post_register_data doesn't exists on WordPress, so it can not be the answer to the question. – cybmeta Feb 5 '16 at 14:34

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.