I had a real problem ... finally it worked !! (after several days of experimenting and hitting his head about it)
One thing to make sure that you have not sent any output yet or that the session cookie will not be written, as it should be in the header. Also, if you call wp_signon before the start of the session, there the session information is also lost ... sheesh ... strange, but I both had it. Anyhoo no more ado ...
// Create a new user (for example)
$userdata = array('user_login'->$username,'user_pass'->$password); $user_id = wp_insert_user($userdata);
// Make sure the username is updated ... I need this because the hook for user_register is called in wp_insert_user, but this hook is called after creating the user, so he needs the DB cache to work clearly, otherwise the wrong username was set ... Thus , the user was automatically registered for only one page - ridiculous. (This was the Plus Redux register, performing bindings by the way.)
wp_cache_delete($user_id, 'users'); wp_cache_delete($username, 'userlogins'); $userdata = get_userdata($user_id); $username = $userdata->user_login;
// Make sure the user session is started.
$vsessionid = session_id(); if (empty($vsessionid)) {session_name('PHPSESSID'); session_start();}
// Login Current user
wp_clear_auth_cookie(); $creds = array(); $creds['user_login'] = $username; $creds['user_password'] = $password; $creds['remember'] = true; $user = wp_signon($creds, false);
// Check the work
if (is_wp_error($user)) {$error = $user->get_error_message();} else { wp_set_current_user($user_id); // The next line *really* seemed to help! do_action('set_current_user'); $current_user = wp_get_current_user(); if (is_wp_error($current_user)) {$error = $user->get_error_message();} } if ($error) {echo $error; print_r($userdata); print_r($current_user);}