1

Is it at all possible to get a user's ID in a function right at the point of log out?

I tried to hook it to wp_logout, but it just returns null when trying to get the user's info.

I'm wondering if there is another hook that comes before wp_logout destroy's the session that would enable me to capture the ID and do something with it.

I simply want to update a user meta field with the time of logout.

Is there any way of doing this?

9

I think I figured this one out.

I had misread this answer and hooked the function I wrote to wp_clear_auth_cookie (actually a function itself!) instead of clear_auth_cookie (the real hook), so that wasn't working. But now using the real hook, I think it might be. Correct me if I'm wrong.

Below is the function with the hook.

function users_last_login() {
    $cur_login = current_time(timestamp, 0);
    $userinfo = wp_get_current_user();
    update_user_meta( $userinfo->ID, 'last_login', $cur_login );
}
add_action('clear_auth_cookie', 'users_last_login', 10);
  • It seems to work but why are your using "login" when what you are actually tracking is "logout"? That is going to be confusing. – s_ha_dum Aug 16 '13 at 14:26
  • I'm tracking their last login logout, which in turn tells them the last time they logged in. That's why I'm using login. If I hook it to the login, the current login will be the user's last login, however, I need it to tell them their last login not including their current one. Does that make sense? – nerdarama Aug 19 '13 at 9:24
  • "I'm tracking their last login logout, which in turn tells them the last time they logged in" -- no it doesn't. It tells you the last time time they logged out, which could be days or weeks after the last login, but that wasn't the point. The comment is about the choice of function names. I am not sold on "self documenting code" but function names should be somewhat descriptive. – s_ha_dum Aug 19 '13 at 13:53
  • The last time they logged in, meaning the last time they were logged in. The site will log you out if you close the browser, so it's never going to be weeks or days after you login. The function's function (no pun intended) is to show the last time they were logged in at, hence users_last_login(). – nerdarama Aug 19 '13 at 14:49

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.