• Resolved mikelast

    (@mikelast)


    stack overflow link reference

    I am having some trouble, I need to update some user meta during the log out process, I also need to update the user meta if the user is automatically logged out for being innactive. I have created a stack exchange question about it but not really having much success. Is anyone able to provide any insight, can this be done, if so how – what hooks do I need to target.

    my existing code is below – it doesnt work, the $expirein value is super low so that I am not waiting forever for the autologout.

    add_filter( 'auth_cookie_expiration', 'keep_me_logged_in_for_1_hour');
    
    function keep_me_logged_in_for_1_hour( $expirein) {
    	$expirein = 120;
    	return $expirein; // 2min
    }
    add_action('clear_auth_cookie', 't_offline_update', 10);
    function t_offline_update() {
    	$userinfo = wp_get_current_user();
    	update_user_meta( $userinfo->ID, 't_online_status', 'offline' );
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    I’m assuming your code works for manual logouts, it’s the auto-logout you have trouble with. This is because there is no auto-logout per se. The auth cookie merely expires, no code executes. Generally it’s best to save data when it changes and not delay until logout. What is the nature of this data where it must wait until logout? Perhaps there’s another way to accomplish your goal.

    Thread Starter mikelast

    (@mikelast)

    You, sir, are an angel – thank you for responding. It never occurred to me that the cookies just expired without any code executing but that makes perfect sense now and explains the reasons why none of my code was properly executing.

    What I need to update is a video chat status, updating from online to offline if the user is away from the computer but forgets to log out. I have adjusted the auth cookie expiry to be about 90 mins. When the user goes through the standard WordPress log out process using wp_logout, I can correctly update the chat status, but I need some way to update it if they are “timed out”

    Moderator bcworkz

    (@bcworkz)

    So you wish to change the status on the server to “offline” after a certain period of inactivity, regardless of what happens client side? This is obviously a common need. I’m not sure how this is typically handled. You could try looking for plugins that need to deal with this same issue and see how they handled it.

    Not knowing anything about what’s normally done, I think what I would do is when the auth cookie is set, I would also use wp_schedule_event() to run script that changes the status when the cookie is due to expire. Anytime the cookie is renewed, the event should be canceled and a new one scheduled to coincide with the new expiration.

    I’m not sure how well this would scale up with a separate event for every user logged in. It’d need to be an extremely busy site for this to be an issue I think. Extreme volume can maybe be dealt with by maintaining blocks of IDs of which to change status for in a series of discrete events. There needs to only be the number of events that fire within the 90 minute window. The event granularity in WP is 10 minutes I believe, so no more than 9 events would be scheduled at any given time with this high volume variant. Finding an ID that needs to be moved because its auth cookie was renewed wouldn’t be too involved with only nine events to check.

    Thread Starter mikelast

    (@mikelast)

    For anyone who stumbles upon this, I have managed to figure out a simple solution so hopefully it will help you as well. Head on over to the stack overflow (link at top)

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Update user meta on logout’ is closed to new replies.