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

1 Answer 1

3

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...
}
3
  • can I use this hook instead of wp_login? Commented Jan 23, 2018 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. Commented Jan 23, 2018 at 6:51
  • It is the best practice from what I see this hook is meant for this.
    – Shibi
    Commented Jan 23, 2018 at 9:50

Your Answer

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

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