0

My goal is to allow my users to select their choice of redirect page after login but it doesn't seem that the user ID is available after login?

Here is my code. Any help is GREATLY appreciated!!!

function my_login_redirect( $redirect_to, $request, $user ) {
    
    $myID = get_current_user_id();
    if ($myID > 0) {
        $my_redirect = xprofile_get_field_data( "Home Page" ,  $myID ); 
        switch($my_redirect) {
            case 'Community':
                return bp_loggedin_user_domain();   
                die;
            case 'Announcements':
                return home_url('/announcements');  
                die;
            case 'Prayers':
                return home_url('/prayers');    
                die;
            case 'Watch':
                return home_url('/watch');  
                die;                
        }
    }
    return home_url();  
    die;
}
 
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );

1 Answer 1

3

As stated in the documentation,

The $current_user global may not be available at the time this filter is run. So you should use the $user global or the $user parameter passed to this filter.

function my_login_redirect( $redirect_to, $request, $user ) {
  // Don't modify the redirect on error.
  if( wp_is_error( $user ) )
    return $redirect_to;

  $myID = $user->ID;

  // ...
}
1
  • Though, it looks like get_current_user_id() actually uses the $user global - this may not be the solution.
    – bosco
    Aug 4 at 1:50

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.