Multisite logout

I want sites to logout when browser tab is closed. Currently using a logout button forces user to reenter name and password. If the browser tab is closed, the user is still logged on.

  • Nebu John
    • FLS

    Hi Natalie ,

    Hope you are doing good and thank you for reaching out to us.

    I couldn’t find an out of the box solution for this. What I can suggest you is to force cookie expiration time using the following code:

    function mywp_change_cookie_logout( $expiration, $user_id, $remember ){
            $expiration = 60; //Time in seconds (60S=1M)
             return $expiration;
    }
    add_filter( 'auth_cookie_expiration','mywp_change_cookie_logout', 10, 3 );

    Use this code in your theme’s functions.php. I hope that helps.

    Kind Regards,
    Nebu John

  • mbitcon
    • Problem solver

    Hi Nebu John

    This really is a tricky one.
    Just a question: If you change the cookie logout expiration time to one minute wouldn’t that also mean that everyone will be logged out after just one minute and has to re-login?
    Also putting the code into the theme’s funtion.php will mean it will be gone after the next theme update.
    I do have to admit that I do not know a solution for this (…yet…:wink: but maybe a few thoughts in which direction to explore further:
    It could be possible to write a very small plugin that loads a bit of Javascript code to the site that could use something like this:
    window.onbeforeunload = closingCode;
    function closingCode(){
    // do something…
    return null;
    }

    Now all you have to do is find a way how to log out via JavaScript…

    Mike

  • Nebu John
    • FLS

    Hi mbitcon ,

    I am sorry, that code will log out every user after a minute.

    Fortunately, we were able to find a plugin that can help you with this. Use the Inactive Logout plugin to automatically terminate idle user sessions, thus protecting the site if the users leave unattended sessions. Auto Logout should happen even after the browser is closed after 1 minute of the browser tab being closed. Please find the download link here: https://wordpress.org/plugins/inactive-logout/

    And about the other question you have asked, yes, the code that we add in a theme functions.php will get overwritten when the theme is updated. You can either use a child theme or mu-plugin to add an additional code when it is needed.

    Kind Regards,
    Nebu John

  • Natalie
    • New Recruit

    Thank you for your support.
    I like the Inactive Logout plugin but, doesn’t solve my problems.
    Users on the site can logout by selecting a logout button. This forces users to re-login. The problem is when a user closes the tab or browser window to get out of the site. I think it has something to do with cookies.
    I tried the a Logout Clear Cookies plugin. It didn’t clear the cookies from the site I was login onto.
    Thank you,

  • Nebu John
    • FLS

    Hi Natalie ,

    As mbitcon pointed, this is a pro feature of the mentioned plugin. Unfortunately, I was unable to find a free plugin that can help you with this.

    mbitcon , I found the following on their website,

    When you close your browser tab without even logging out of the WordPress site. Inactive Logout Pro will logout the user automatically after 2 minutes of tab close duration.

    So I believe it will work even when the browser tab is closed. However, please note, I have not tested this as I don’t have a pro version of this plugin.

    Kind Regards,
    Nebu John