I have seen some answers, but the questions are quite a number of years old, and the after trying their responses I've not been able to get it to work. I need to update user meta data during the logout process, specifically the automatic logout process that occurs when the user is idle for too long. I am using the code below but the user meta is not being updated.

add_action('clear_auth_cookie', 't_o_update');
function t_o_update() {
  $user = wp_get_current_user();
  update_user_meta($user->ID, 'online_status', 'offline');

can anyone help with this, can anyone explain how I can debug this? Does anyone have a solution. many thanks

  • 1
    WP already has a basic sessions tracker, you don't need to create your own. Also keep in mind that a user can have multiple sessions, just because they logged out on their phone doesn't mean they're logged out on their PC or their laptop, it's literally just clearing the auth cookie
    – Tom J Nowell
    Sep 13 '17 at 16:25
  • the online_status has nothing to do with wordpress, it is a completely decoupled status, it relates to starting/stopping video Sep 14 '17 at 7:54
  • I mention it because the system described in your question won't work and assumes there is only 1 session active at any time, when infact there can be multiple, either via different browsers, or different devices. As a result you can open it up on a phone to go online, then log into 5 other devices and log out of the phone. Now the users online_status is offline, yet they're online on the other 5 devices, breaking the system
    – Tom J Nowell
    Sep 14 '17 at 15:52

For anyone who stumbles upon this, I have found a solution that works well for me. I wanted to be able to update user meta data during the logout process, whether it is user initiated or automatic through expiry of session data. So the solution:

function logMeOutOrSomething($expiration, $user_id) {
    update_user_meta($user_id, 'first_name', 'superMario');
    return $expiration;
add_filter('auth_cookie_expiration', 'logMeOutOrSomething', 10, 2)

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.