WordPress.org

Ready to get started?Download WordPress

Forums

How do you get $user_id from inside login_redirect filter? (9 posts)

  1. SFC
    Member
    Posted 3 years ago #

    I've spent all day searching the forum trying to figure out what I'm doing wrong would someone please take a look.

    How do I get the $user_id when inside the login_redirect filter?

    I'm using the below code but $user_id returns with nothing in it. What I'm I doing wrong?

    function childtheme_redirect_to_blog() {
    	global $current_user;
    	get_currentuserinfo();
    	$user_id == $current_user->ID;
    
    	$flg = get_user_meta( $user_id ,'profile_redirect_flg' , true);
    	if ( $flg )   {
    		$redirect_to	= "http://" . $_SERVER['HTTP_HOST'] ;
    		return $redirect_to;
    	} else {
    		$redirect_to	= "http://" . $_SERVER['HTTP_HOST'] . "/wp-admin/profile.php";
    		return $redirect_to;
    	}
    }
    add_filter('login_redirect','childtheme_redirect_to_blog');

    Any clue would be greatly appreciated. I'm using the meta flag to determine if this is a user's first login. It is my understanding that the login_redirect filter executes after the user is authenticated; is that not correct?

    Thank you,
    SFC

  2. Mark / t31os
    Moderator
    Posted 3 years ago #

    This line looks incorrect.

    $user_id == $current_user->ID;

    If you're assigning a value to a variable, you should have a single equals, eg..

    $user_id = $current_user->ID;

    A double equals would usually be seen inside a conditional statement that checks if two values are equal to one another.

    For example.

    // Single equals when assigning a value
    $a = 2;
    $b = 4;
    
    // Double equals when doing a value comparision
    if( $a == $b )
      echo '$a is equal to $b';
    else
      echo '$a is not equal to $b';

    Hope that helps.. :)

  3. SFC
    Member
    Posted 3 years ago #

    Mark, That was a recently introduced mistake, sorry for the typo. Would you please take another look. I made the correction you pointed out and the $user_id is returning 0. It should be returning with $user_id = 7 for the user being logged in.

    If the login_redirect filter does not execute after authentication then which should I use?

    Thank you,
    SFC

  4. Mark / t31os
    Moderator
    Posted 3 years ago #

    For that particular filter you don't need to grab the current user data, just globalise the $user var (it's available at that point having looked where that filter is set)..

    Eg.

    function childtheme_redirect_to_blog() {
    	global $user;
    
    	$user_id = $user->ID;
    
    .. trimmed for example

    Hope that helps.. :)

  5. SFC
    Member
    Posted 3 years ago #

    That did the trick. Thank you Mark. You're a life saver.
    SFC

  6. Mark / t31os
    Moderator
    Posted 3 years ago #

    You're welcome.. ;)

  7. Tom Auger
    Member
    Posted 3 years ago #

    Looking at the source for login_redirect:
    $redirect_to = apply_filters('login_redirect', $redirect_to, isset( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '', $user);, one notes that the $user global variable is available as a parameter passed to the filter hook.

    So, if you're a purist and wish to avoid using the messy global variable, just make it an argument for your filter function:

    function childtheme_redirect_to_blog($user) {

  8. Tom Auger
    Member
    Posted 3 years ago #

    Whoops, my bad. You actually get up to 3 args, of which $user is the last:

    function childtheme_redirect_to_blog($redirect, $request, $user) {

    and don't forget that if you want to take advantage of these extra args, you have to allow your filter hook to accept them:

    add_filter('login_redirect','childtheme_redirect_to_blog',10,3);

    (here 10 is just the priority, which we're providing so we can get to the 3).

  9. Mark / t31os
    Moderator
    Posted 3 years ago #

    You're missing the point of that global statement, it simply gives the variable scope inside the function, there's nothing *messy* about it. However i'll agree insofar that, if the variable is passed by the hook, then yes it's preferable to use that instead(imho).

Topic Closed

This topic has been closed to new replies.

About this Topic