apply_filters( ‘auth_cookie_expiration’, int $length, int $user_id, bool $remember )

Filters the duration of the authentication cookie expiration period.

Parameters

$lengthint
Duration of the expiration period in seconds.
$user_idint
User ID.
$rememberbool
Whether to remember the user login. Default false.

Source

$expiration = time() + apply_filters( 'auth_cookie_expiration', 14 * DAY_IN_SECONDS, $user_id, $remember );

Changelog

VersionDescription
2.8.0Introduced.

User Contributed Notes

  1. Skip to note 5 content

    An example of how to extend time in your WordPress session by a year, simply enter this code into your `functions.php` or plugin. Other durations have been added to show different times hat can be set.

    add_filter ( 'auth_cookie_expiration', 'wpdev_login_session' );
    
    function wpdev_login_session( $expire ) { // Set login session limit in seconds
        return YEAR_IN_SECONDS;
        // return MONTH_IN_SECONDS;
        // return DAY_IN_SECONDS;
        // return HOUR_IN_SECONDS;
    }
  2. Skip to note 6 content

    Extension of the example, only extending expiration if $remember and low privelege.

    add_filter('auth_cookie_expiration', 'auth_cookie_expiration_filter_5587', 10, 3);
    function auth_cookie_expiration_filter_5587($expiration, $user_id, $remember) {
        if ($remember && !user_can($user_id, 'edit_others_posts')) {
            return YEAR_IN_SECONDS;
            // return MONTH_IN_SECONDS;
            // return DAY_IN_SECONDS;
            // return HOUR_IN_SECONDS;
        }
        // default
        return $expiration;
    }
  3. Skip to note 8 content
    function wp_homework_change_cookie_logout( $expiration){
    	$user = wp_get_current_user();
    	$allowed_roles = array('administrator', 'scholar');
    		 if( array_intersect($allowed_roles, $user->roles ) ) { 
    		   $expiration = 31557600;
    		 } 
        return $expiration;
    }
    
    add_filter( 'auth_cookie_expiration','wpse108399_change_cookie_logout', 10, 3 );

You must log in before being able to contribute a note or feedback.