Set Custom Session Expiration and Auto-Logout in WordPress

Improve your website security posture by controlling user session duration through the auth_cookie_expiration filter. Override default WordPress login persistence by returning time in seconds for custom timeouts, such as 3600 seconds for a one-hour session. This mechanism effectively forces an auto-logout once the authentication cookie expires, reducing the risk of unauthorized access on shared computers or high-security administrator accounts. Implementing custom cookie expiration ensures that sessions end after a defined period of inactivity, even if the "Remember Me" checkbox was selected during login.

Auto Logout Users with WordPress Auth Cookie Expiry

Advanced Guides Code Snippets Coding Blog PHP PHP Debugging PHP Snippets Theme Customization Tutorials Wordpress WordPress Code Snippets WordPress Development WordPress Functions WordPress How-To

Auto Logout Users with WordPress Auth Cookie Expiry Tutorial/Guide

According to a 2021 IBM study, human error accounts for 95% of security breaches. One such oversight — staying logged in on high-access accounts — can be tackled by setting WordPress auth cookie expiration. Auto-logout mechanisms help eliminate such risks, forcing session timeouts after user inactivity. Let’s explore how to configure authentication cookie settings in WordPress.

Steps to Configure WordPress Auth Cookie Timeout

From banking to blogging, the idea of session timeout has become standard. WordPress lets you control cookie expiration using either plugins or code. While plugins are convenient, they can slow your site and require constant updates. A better approach is to apply a custom function in your theme. To enforce login expiration, use the following code inside your functions.php file:

/* Set session timeout to 1 hour */
add_filter( 'auth_cookie_expiration', 'keep_me_logged_in_for_1_hour', 9999, 1 );
function keep_me_logged_in_for_1_hour( $expirein ) {
    return 60*60; 
}

Why Use Authentication Cookie Expiry?

WordPress generally ends login sessions after browser closure. However, enabling the “remember me” option can keep users signed in — even if they’re inactive. This snippet overrides that behavior by enforcing a custom auth cookie timeout through the WordPress REST API system — enhancing your site’s defense posture.  

๐Ÿ’ก Have a Coding Problem?

Search our archives or reach out to our team for solutions and expert advice.