TransWikia.com

WordPress auto login after registration not working

WordPress Development Asked by Overcode on October 30, 2021

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?

5 Answers

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

Answered by butlerblog on October 30, 2021

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 );
}

Answered by user1075397 on October 30, 2021

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.

Answered by youcantexplainthat on October 30, 2021

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'] );
}

Answered by matthew on October 30, 2021

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.

Answered by phpsmashcode on October 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP