1

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.

Brett Canfield
  • 71
  • 1
  • 12

1 Answers1

2

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'); 
Maha Dev
  • 3,915
  • 2
  • 31
  • 50
  • That does not work. It still asks me to login (the path I redirect them to is not accessible unless they are logged in) I also use a custom login screen, would that make any difference (I redirect away from wp-login.php) – Brett Canfield Mar 03 '17 at 06:39
  • After login script, try to redirect at some other page that do not required login. and then go to any loggedin page. – Maha Dev Mar 03 '17 at 07:02
  • That doesn't seem to make a difference either. The user is still not logged in at this time. – Brett Canfield Mar 05 '17 at 22:37
  • The issue was caused by javascript code that was leaving most pages where they were not in a position to be able to write cookies. I have since fixed this and it is no longer an issue – Brett Canfield May 12 '17 at 00:32
  • GREAT ... save my day.. why we need last line.if we need double check in every refresh pages and check the CAS ID or OAUTH token in COOKIES in every visit do we need to removed that? – saber tabatabaee yazdi Dec 18 '18 at 06:58