Support » Plugins » Hacks » How do you get $user_id from inside login_redirect filter?

Viewing 8 replies - 1 through 8 (of 8 total)
  • 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';
      echo '$a is not equal to $b';

    Hope that helps.. 🙂



    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,

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


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

    Hope that helps.. 🙂



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

    You’re welcome.. 😉

    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) {

    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:


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

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

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How do you get $user_id from inside login_redirect filter?’ is closed to new replies.