Security PHP Beginner

Shorten Login Sessions for Staff

Log out editors and administrators after 8 hours, or 2 days with "Remember me", instead of WordPress's 2 and 14 days.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 7 lines
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 );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

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.

How it works

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.

Things to watch

  • When a session expires in wp-admin, WordPress shows a login pop-up so work in progress isn’t lost.
  • To end every existing session now, use “Log Out Everywhere Else” on a user’s profile.

How to test it

Log out and back in as an administrator. In DevTools → Application → Cookies, the wordpress_logged_in_ cookie should expire about 8 hours from now.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only