Log out editors and administrators after 8 hours, or 2 days with "Remember me", instead of WordPress's 2 and 14 days.
add_filter( 'auth_cookie_expiration', function ( int $length, int $user_id, bool $remember ): int {
// Only shorten sessions for people who can edit content.
if ( ! user_can( $user_id, 'edit_posts' ) ) {
return $length;
}
return $remember ? 2 * DAY_IN_SECONDS : 8 * HOUR_IN_SECONDS;
}, 10, 3 );
Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.
WordPress keeps people logged in for 2 days, or 14 days with “Remember me”. For accounts that can change your site, a stolen or shared computer can stay logged in for a long time. This snippet shortens sessions for staff only.
The auth_cookie_expiration filter sets how long the login cookie lasts. For users who can edit posts, which covers contributors up to administrators, it becomes 8 hours, or 2 days with “Remember me”. Customers and subscribers keep the normal lengths.
The new length applies from the next login. Existing sessions keep their old expiry.
Log out and back in as an administrator. In DevTools → Application → Cookies, the wordpress_logged_in_ cookie should expire about 8 hours from now.