These days I needed to extend the lifespan of the authentication cookie, and I thought I share with you how to do that.
To make this change you need to apply a filter, so add the following snippet in functions.php
:
function wcr_change_cookie_expiration($expiration, $user_id, $remember) { $expiration = 100 * DAY_IN_SECONDS; // means 100 days and the default value is 14 days return $expiration; } add_filter('auth_cookie_expiration', 'change_cookie_expiration', 10, 3);
Alternatively, if the user has checked the “Remember Me” on login page, use this snippet:
function wcr_change_cookie_expiration($expiration, $user_id, $remember) { if ($remember) { $expiration = 100 * DAY_IN_SECONDS; // means 100 days and the default value is 14 days } return $expiration; } add_filter('auth_cookie_expiration', 'change_cookie_expiration', 10, 3);
Using this filter also gives you access to the $user_id variable
, so you can use it to change the expiration time based on user role or any other user-specific data (see get_user_meta
).
Thanks. However I think your filter should be:
add_filter(‘auth_cookie_expiration’, ‘wcr_change_cookie_expiration’, 10, 3);