functions - Prevent Wordpress Automatic Logout

admin2025-06-02  1

I am using this function which is supposed to prevent wordpress from automatically logging me out. However, I am still being logged out every so often.

function my_logged_in( $expirein ) {
   return 31556926; // 1 year in seconds
}
add_filter( 'auth_cookie_expiration', 'my_logged_in' );

Is there another solution to this?

I am using this function which is supposed to prevent wordpress from automatically logging me out. However, I am still being logged out every so often.

function my_logged_in( $expirein ) {
   return 31556926; // 1 year in seconds
}
add_filter( 'auth_cookie_expiration', 'my_logged_in' );

Is there another solution to this?

Share Improve this question asked Feb 27, 2019 at 12:34 JoaMikaJoaMika 6986 gold badges27 silver badges58 bronze badges 2
  • Have you verified in your browser dev tools that the cookie expiration is indeed being set? – Tom J Nowell Commented Feb 27, 2019 at 12:43
  • Also, there are predefined macros for time lengths, e.g. return YEAR_IN_SECONDS;. Additionally, that filter also passes if the user checked remember me, but that functionality would be broken with that filter. Where is your filter placed? functions.php? A plugin? Is it itself ran on a hook? Have you verified that it gets called? – Tom J Nowell Commented Feb 27, 2019 at 12:43
Add a comment  | 

2 Answers 2

Reset to default 1

As we know by default WordPress keeps logged in for 48 Hours. If we check "Remember me" while login then it will keep login for 14 Days.

If you want to set the logout timeout you can use this code as:

function wpset_change_cookie_logout( $expiration, $user_id, $remember ) {
    if( $remember && user_can( $user_id, 'manage_options' ) ){
        $expiration = 31556926;
    }
    return $expiration;
}
add_filter( 'auth_cookie_expiration','wpset_change_cookie_logout', 10, 3 );

Did you take a look at your session.gc_maxlifetime in your php settings? Also, some operating systems like Debian or Ubuntu have a crontab that flushes sessions. More info here

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748860149a314322.html

最新回复(0)