WordPress Logout Only If User Click Logout or If User Delete Browser History

You can use the auth_cookie_expiration filter to change the expiration time of the cookie WordPress sets to remember you. The user won’t be logged out unless they change browser or clear their cookies (normally part of clearing history).

The problem is that you can’t set a cookie to never expire, so you have to set a date in the far future. The furthest you can go is 19th January 2038, because of the Year 2038 problem.

The value of the auth_cookie_expiration filter is added to time() so if you want to set the longest possible time for the cookie, you need to get the maximum value (2147483647 according to this) and subtract time():

function wpse_287104_cookie_expiration( $length ) {
    return time() - 2147483647;
}
add_filter( 'auth_cookie_expiration', 'wpse_287104_cookie_expiration' );

Leave a Comment