auth_cookie_expiration
Allows you to change the lifetime of cookies used to authenticate users.
Usage
add_filter( 'auth_cookie_expiration', 'wp_kama_auth_cookie_expiration_filter', 10, 3 );
/**
* Function for `auth_cookie_expiration` filter-hook.
*
* @param int $length Duration of the expiration period in seconds.
* @param int $user_id User ID.
* @param bool $remember Whether to remember the user login.
*
* @return int
*/
function wp_kama_auth_cookie_expiration_filter( $length, $user_id, $remember ){
// filter...
return $length;
}
- $length(int)
- Cookie lifetime in seconds.
- $user_id(int)
- User ID.
- $remember(true/false)
- Whether the Remember Me checkbox was selected during authentication. Remembering the user normally increases the cookie lifetime.
Default: false
Examples
#1 Extend a session based on the user's role
Extend an administrator's session to 20 days when Remember Me is selected, and to 5 days when it is not.
For all other users, extend sessions to half a year when Remember Me is not selected, and to a year when it is selected.
add_filter( 'auth_cookie_expiration', 'cookie_expiration_new', 20, 3 );
function cookie_expiration_new ( $expiration, $user_id, $remember ) {
// Cookie lifetime for an administrator.
if ( $remember && user_can( $user_id, 'manage_options' ) ) {
// Remember Me is selected.
if ( $remember == true ) {
return 20 * DAY_IN_SECONDS;
}
// Remember Me is not selected.
return 5 * DAY_IN_SECONDS;
}
// For all other users.
// Remember Me is selected.
if ( $remember == true ) {
return 360 * DAY_IN_SECONDS;
}
// Remember Me is not selected.
return 180 * DAY_IN_SECONDS;
}
#2 Double the current session lifetime for all users
add_filter( 'auth_cookie_expiration', 'my_auth_cookie_expiration', 20, 3 );
function my_auth_cookie_expiration( $expiration, $user_id, $remember ) {
return $expiration * 2;
}Changelog
| Since 2.8.0 | Introduced. |
Where the hook is called
auth_cookie_expiration
auth_cookie_expiration
wp-includes/pluggable.php 1082
$expiration = time() + apply_filters( 'auth_cookie_expiration', 14 * DAY_IN_SECONDS, $user_id, $remember );
wp-includes/user.php 2949
$default_cookie_life = apply_filters( 'auth_cookie_expiration', ( 2 * DAY_IN_SECONDS ), $user_id, false );
wp-includes/pluggable.php 1091
$expiration = time() + apply_filters( 'auth_cookie_expiration', 2 * DAY_IN_SECONDS, $user_id, $remember );