The problem

WordPress comes with a login mechanism – stored in wp-login.php (installed in your base folder, typically).  There is a case I came across where a user would do the following:

  1. Navigate to pagea.php (this page has some content that is shown when logged in).
  2. Navigate to pageb.php and login.
  3. Navigate back to pagea.php.

At this point, you would expect pagea.php to show the content that is special for users that are logged in, but it does not.  Why is that?

The cause

WordPress has a function – is_user_logged_in().  You might see some code that looks something like this:

if ( is_user_logged_in() )
{
   ;  // Show some special content
}
else
{
   ;  // Show nothing
}

As it turns out, in the problem posed above, the function “is_user_logged_in” returns ‘false’ after the successful login attempt.  And the reason that is has to do with the WordPress variable $current_user is not filled out.

The Fix

Various sites have made suggestions on WP_CACHE, COOKIEPATH, and wp_login.  Nothing worked for me.  After testing a number a things, I came up with this code:

function action_init()
{ 
 // After logging in, if a user navigates to another page,
 // it typically does not fill in $current_user; for whatever
 // reason, this call to get_currentuserinfo fills that in
 // and makes is_user_logged_in() work as expected..
 get_currentuserinfo();
 nocache_headers();
}
add_action( 'init', 'action_init', 1 );

Basically, I manually tell WordPress to fill in the $current_user variable whenever WordPress is loaded since it appears to have a problem doing this automatically in all cases.  I expect the load time to be negligible.