1

I want to get current user's session token in the hook - wp_login.

I have tried this code in functions.php:

 add_action('wp_login','test');
  function test() {
  var_dump(wp_get_session_token()); //string(0) "" 
  exit;
    }

I am using wordpress 4.9.2

2

You can't access the $_COOKIE in this hook. You can only access $_COOKIE after the page HTTP headers are set with the $_COOKIE its mean in the next page.

You have other hook that you can hook into to get the cookies in the function wp_set_auth_cookie() there is this action at the end.

do_action( 'set_logged_in_cookie', $logged_in_cookie, $expire, $expiration, $user_id, 'logged_in', $token );

So you can use it like this:

add_action('set_logged_in_cookie', 'custom_get_logged_in_cookie', 10, 6);
function custom_get_logged_in_cookie($logged_in_cookie, $expire, $expiration, $user_id, $logged_in_text, $token) {
    // do something...
}
| improve this answer | |
  • can I use this hook instead of wp_login? – Faiyaz Alam Jan 23 '18 at 6:25
  • basically I want to save session_token just after login. I do not want to use $_SESSION variable. wp_login does not provide session_token but set_logged_in_cookie provides. So I can use it. Please let me know if it is a wrong method to achieve this. – Faiyaz Alam Jan 23 '18 at 6:51
  • It is the best practice from what I see this hook is meant for this. – Shibi Jan 23 '18 at 9:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.