how to change session expire time in wordpress

2019-02-01 23:03发布

问题:

I want expire the session if user (admin) is inactive for 15 minute in wordpress site,

can anyone tell me what is the default session expiry time in wordpress? and how to change that default expire time.

回答1:

You need to do it manually. Unfortunately Wordpress doesn't have any option for doing this.

To do that by yourself see this similar question on SO.

Or you can try installing this plugin that allows you to change Wordpress default session timeout value.



回答2:

Simply add this code in your theme's functions.php:

add_filter('auth_cookie_expiration', 'my_expiration_filter', 99, 3);
function my_expiration_filter($seconds, $user_id, $remember){

    //if "remember me" is checked;
    if ( $remember ) {
        //WP defaults to 2 weeks;
        $expiration = 14*24*60*60; //UPDATE HERE;
    } else {
        //WP defaults to 48 hrs/2 days;
        $expiration = 2*24*60*60; //UPDATE HERE;
    }

    //http://en.wikipedia.org/wiki/Year_2038_problem
    if ( PHP_INT_MAX - time() < $expiration ) {
        //Fix to a little bit earlier!
        $expiration =  PHP_INT_MAX - time() - 5;
    }

    return $expiration;
}