8

I use wp_signon() and it returns a user, not an error. However when I do is_user_logged_in() it returns false.

Help would be very much appreciated :)

5 Answers 5

6

get_currentuserinfo() didn't work for me. I've written about this problem and solution at my blog:

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

Cheers, Rhys

3
  • 2
    This really helped. In case the blog goes missing, he said to use wp_set_current_user() after wp_signon().
    – d79
    Oct 15, 2015 at 17:14
  • Worked for me bro.
    – user107771
    Jul 25, 2018 at 6:37
  • get_currentuserinfo() is now deprecated, however wp_get_current_user() works for me. Mar 1, 2019 at 22:52
5

After using wp_signon(), the user info is not set, which is how WP checks for a user in is_user_logged_in(). It should be just a matter of calling get_currentuserinfo() after wp_signon().

2
  • Is it possible to replicate the user having signed in through the login form. I would like the user to stay logged in, as with regular log in.
    – Benedicte
    Nov 4, 2010 at 14:13
  • 1
    try $creds = array(); $creds['user_login'] = $user_login; $creds['user_password'] = $user_pass; $creds['remember'] = true; $user = wp_signon( $creds, false ); to remember the user Nov 17, 2011 at 19:49
1

I've had same problem. Here the full working snippet that fixed that problem:

    if( isset($_POST['log']) && isset($_POST['pwd']) ):
      $creds = array( 'user_login' =>  $_POST['log'], 'user_password' => $_POST['pwd'], 'remember' => $_POST['rememberme'] );
      $user = wp_signon( $creds, false );
      if ( is_wp_error($user) ): echo $user->get_error_message(); endif;
      wp_set_current_user($user->ID);
      return $user;
    endif;

Also wp_logout() has same problem. Here how to make it work too:

wp_logout();  
wp_set_current_user(0);
1
 //the function wp_signon is under construction yet if you open wordpress core files
 //you will find this comment

 // TODO do we deprecate the wp_authentication action?

 //i figured out a solution
 function login_after_register($userlogin,$userpass){
$credentials = array( 'user_login' =>  $userlogin, 'user_password' => $userpass, 'remember' => true );

$secure_cookie = is_ssl();

$secure_cookie = apply_filters('secure_signon_cookie', $secure_cookie, $credentials);
add_filter('authenticate', 'wp_authenticate_cookie', 30, 3);

$user = wp_authenticate($credentials['user_login'], $credentials['user_password']);
wp_set_auth_cookie($user->ID, $credentials["remember"], $secure_cookie);
do_action('wp_login', $user->user_login, $user);
 }
 //then you should call it this way
 //before get_header() or any html
 login_after_register($_POST['r_user_name'],$_POST['r_password']);
1
  • 5
    If this is supposed to be an answer, it needs some explanation. Please update your answer.
    – fuxia
    May 26, 2013 at 21:41
1

Just set the second parameter to true. Set false will prevent wp_signon() from setting secure cookie which is essential for accessing wp-admin.

$user = wp_signon( $credentials, true );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.