WordPress: wp_signon() – $current_user is not populated

Happy Christmas and holidays!

I would be out in the sun if there was any! But since it’s raining I’m doing a bit of WordPress development for a project I’m working on.

The wp_signon() function logs a user in but for some reason after doing so the global user variables such as $current_user and $user_ID are not populated until the page is refreshed and calling get_currentuserinfo() doesn’t populate them. The is_user_logged_in() function also returns false.  I think the problem has something to do with the cookie authentication process.

There is however a solution to the problem and it doesn’t involve refreshing the page.  When you call wp_signon() it returns a user object if the sign-on was successful, so we do have all the user info we need, it’s just not available in the standard global way.  To fix this we just need to call the wp_set_current_user() specifying the id of user object which wp_signon() returned.

To demonstrate, here’s an example of creating a user, logging them in and submitting a post all in one page refresh.

//Register a new user
function _my_newuser($username,$password,$email,$nickname)
	global $wpdb;
	$user_login = $wpdb->escape( $username );
	$user_pass = $password;
	$nickname = $nickname;
	$user_email = $username;
	$userdata = compact('user_login', 'user_email', 'user_pass', 'first_name','nickname' );
	return wp_insert_user($userdata);
//Log a user in and set them as current user
function _my_user_login($username,$password)
	$creds = array();
	$creds['user_login'] = $username;
	$creds['user_password'] = $password;
	$creds['remember'] = true;
	$user = wp_signon( $creds, false );
	wp_set_current_user($user->ID); //Here is where we update the global user variables
	return $user;
//Commits a new post to the DB
function _my_commit_post($postdata)
	global $user_ID;
	$new_post = array(
		'post_title' => $postdata->title,
		'post_content' => $postdata->description,
		'post_status' => 'publish',
		'post_date' => date('Y-m-d H:i:s'),
		'post_author' => $user_ID,
		'post_type' => 'post',
		'post_category' => array(0),
		'tags_input' => $postdata->tags
	$post_id = wp_insert_post($new_post,true);
//Print info about the user who is now logged in
function _my_current_user()
	global $current_user,$user_ID;
		echo '<br />User Logged in ok<br />';
		echo 'User ID is: '.$user_ID.'<br />';
		echo 'User login is: '.$current_user->user_login.'<br />';
		echo 'No user is logged in< br/>';

Of Course you might want some error handling in there! 😉 And did you notice the new syntax highlighting plug-in? Neat ay?

, , , , ,

11 Responses to WordPress: wp_signon() – $current_user is not populated

  1. Jeremytync June 20, 2016 at 12:59 am #

    выездной мобильный шиномонтаж жуков – шишка на колесе ремонт, круглосуточно шиномонтаж.

  2. Mahak November 1, 2015 at 5:14 am #

    Thanks, Its working on that page but the problem is when we open another page it shows that user is not logged in??

    Any solution to keep user logged in throughout the site.

  3. Keith Lawless April 2, 2015 at 7:41 am #

    Thanks for this oldie but goodie. Works like a champ. Now I can go unravel the spaghetti code I just wrote trying out different action hooks looking for when the user cookie was available.

  4. Matt Stone July 18, 2014 at 8:29 pm #

    THANK YOU!!!

    I was tearing my hair out trying to generate a new nonce after logging in a user via AJAX – it kept returning the same “logged out” nonce.

  5. vfhky March 9, 2013 at 4:37 am #

    i think we should use isset($_COOKIE[‘comment_author_email_’.COOKIEHASH]) instead of is_user_logged_in(), because it works perfect!

  6. Laaki February 11, 2013 at 5:49 am #

    This was driving me crazy. There is no documentation in wordpress codex database. Worked like a charm in my new plugin. Thanks a lot.

  7. Brian April 29, 2012 at 10:12 am #


  8. Andy Potanin January 12, 2011 at 6:26 am #

    You may also encounter problems because get_currentuserinfo() will blank out $current_user in certain situations – if you are making an admin-ajax.php call and is_admin() is true, regardless of if you use priv or nopriv mode, for example.

    In any case, to solve do: wp_signon( $creds, true); to set a secure cookie.

  9. RhysGoodwin December 27, 2010 at 10:09 pm #

    New Post: WordPress: wp_signon() – $current_user is not populated – https://blog.rhysgoodwin.com/programming/


  1. wp_signon returns user, but the user is not logged in – Read For Learn - April 8, 2022

    […] http://blog.rhysgoodwin.com/programming/wordpress-wp_signon-current_user-is-not-populated/ […]

Leave a Reply