Reputation: 843
I'm trying to add a user id into a cookie. There appear to be a lot of posts on the subject. All are based around the following assignment
function TheFunction() {
global $current_user;
get_currentuserinfo();
$id = $current_user->ID;
setcookie('cookiename', 'hello'.$id, time()+3600*24*100, COOKIEPATH, COOKIE_DOMAIN, false);
}
add_action( 'wp_login', 'TheFunction');
The hook is working and passing the value into the cookie, but failing to return any user parameters (e.g. my cookie value is hello0). I've tried wp_login and wp_authenticate to ensure that a $current_user exists. I'm now running our of ideas.
Can you help? Thanks
Upvotes: 0
Views: 80
Reputation: 6976
It looks like, for whatever reason, the $current_user
cannot be populated at that particular point, even though the user has just logged in. You can work around this by using the user object that is passed to the wp_login
action.
add_action('wp_login', function($username, $user) {
setcookie('cookiename', 'hello'. $user->ID, time()+3600*24*100, COOKIEPATH, COOKIE_DOMAIN, false);
}, 10, 2);
Upvotes: 1