简体   繁体   中英

wp_set_auth_cookie not logging on user

I am trying to add a function to bp_core_activated_user, where I want it to do the following:

  1. Activate the user
  2. Login the user
  3. Redirect the user to their profile edit screen

I don't have any issues with 1. and nor with 3, but since the second step is not working, it will not do #3 properly.

Here is my code example

function auto_login_activation($user_id, $key, $user) {
$bp = buddypress();
$bp->activation_complete = true;
//now login and redirect

$user = get_user_by( 'id', $user_id ); 
if( $user ) {
    wp_set_current_user( $user_id, $user->user_login );
    wp_set_auth_cookie( $user_id );
}

bp_core_redirect( bp_core_get_user_domain( $user_id ) .$bp->profile->slug .'/edit/' );
}
add_action( 'bp_core_activated_user', 'auto_login_activation', 0, 3 );

If I disable the redirect, it shows on my screen as logged in (will show Log Out, and hide Log In and Sign Up) but when I go to a page that is only for logged in users, it states I am logged out.

I know step 1 is working, as the user receives an automated welcome email on activation. Step 2 is where it falls down, and step 3, I use the bp_core_redirect elsewhere within my code without issues I had been using a plugin for this purpose, but it also stopped working, and is basically the same code.

You need to use wp_login action hook like this :

do_action('wp_login','username','user_email');
wp_set_current_user('user_id');
wp_set_auth_cookie('user_id');
wp_redirect('url'); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM