Get user’s ID on logout

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.

Read More

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?

Related posts

1 comment

  1. 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);
    

Comments are closed.