I work from home so can afford to leave tabs open for each of my WordPress sites. That way I can jump on anytime and update or add new content very quickly. The problem I kept running into is that WordPress automatically logs out users after 48 hours. Which means I have to log back in every day even when it’s not necessary. So I needed a way to stay logged in to WordPress indefinitely. Fortunately WordPress is very flexible and easy to customize, and the login duration can be changed via several different methods.
Here are three easy ways to stay logged in to WordPress for a longer period of time.
Three ways to do it..
Check the box
The easiest way to increase the expiration date/time for logins, is to simply check the “Remember Me” checkbox when logging in to WordPress. That will increase the expiration to 14 days, or whenever the browser is closed. After that time, the session cookie expires and you’ll need to log in once again.
This is useful if 14 days is enough time for your workflow.
One downside is that it requires an extra click to check the box. Fine I guess if you’re logging in manually. But if you’re using a password manager or other auto-login app, the extra checkbox step requires action on your part, thus adding friction and slowing things down.
Another downside is that 14 days is not always enough. For my own workflow, I prefer to minimize as many needless steps as possible. So I prefer the next method of extending the login duration, using a slice of custom code..
Add custom code
For more flexibility and less friction, you can add the following code snippet to stay logged in to WordPress for however long is necessary, even indefinitely if it makes sense to do so. This is the preferred technique for my own websites.
Important: Be mindful of any other users who may be logging in on public machines. Only extend the login duration if you know 100% that it’s safe and secure.
Here is the magic code to stay logged in to the WordPress Admin Area. You can add this code via your theme functions file, or add via simple custom plugin. Here is a guide that explains how to do both.
function shapeSpace_stay_logged_in($expires) {
return 172800; // default 48 hours
}
add_filter('auth_cookie_expiration', 'shapeSpace_stay_logged_in');
As written, this code hooks into auth_cookie_expiration and filters the expiration duration (in seconds). By default the duration is 48 hours. You can change that to anything that works best.
To stay logged in forever, change the interval to some very large number, like 3153600000
to stay logged in for 100 years ;) To help with converting time to seconds, you can use a free time conversion calculator.
Thanks to Alex Mills (Viper007Bond) for sharing this code at
Stack Exchange.
Update! Thanks to Scott Fennell for pointing out that WordPress provides a set of time constants that we can use instead of typing out seconds like animals:
MINUTE_IN_SECONDS
HOUR_IN_SECONDS
DAY_IN_SECONDS
WEEK_IN_SECONDS
MONTH_IN_SECONDS
YEAR_IN_SECONDS
So we can do like 3 * HOUR_IN_SECONDS
to specify a time interval of 3 hours.
Bonus: Check the “Remember” box by default
Bonus tip! To automatically check the “Remember Me” box on the WP Login Page, add the following code via theme functions or custom plugin.
function shapeSpace_custom_login_checkbox() {
?>
<script>
document.getElementById('rememberme').checked = true;
document.getElementById('user_login').focus();
</script>
<?php
}
add_filter('login_footer', 'shapeSpace_custom_login_checkbox');
No changes need made, just add and done. One in place, the remember checkbox will be selected automatically by default.
Install a plugin
If you want to extend the login beyond 14 days, but don’t want to go the custom code route, installing a plugin is the way to go. Currently there seems to be only a couple of capable plugins in the WP Plugin Directory:
Let me know if I’ve missed anything! :)