I work from home, so I can keep multiple tabs open for each of my WordPress websites. This allows me to quickly make updates or add new content whenever I want. However, I faced an issue where WordPress automatically logged me out after 48 hours, so I had to log in again every day, even when I didn’t need to. To avoid this, I wanted to find a way to stay logged in to WordPress for a longer time. Luckily, WordPress is flexible and customizable, and there are different ways to change the duration of your login session.
You can stay logged in to WordPress for a longer time using three simple methods:
- Check the Box: Just tick a box in your WordPress settings.
- Add Custom Code: Insert some special code into your WordPress.
- Install a Plugin: Use a WordPress plugin to handle it for you.
Check the box
The simplest way to make your WordPress login last longer is to click the “Remember Me” box when you log in. This will extend your login session to either 14 days or until you close your web browser. After this time, you’ll have to log in again.
One drawback is that it needs an extra click to tick the box. It’s not a big deal if you log in by typing your password, but if you use a password manager or a tool that logs you in automatically, this extra step can slow you down.
Also, 14 days may not be long enough for some people. Personally, I like to make things as straightforward as possible, so I prefer the next method, which involves using a bit of custom code to extend the login time.
Add custom code
If you want more control and don’t want any extra steps, you can use this code snippet. It lets you stay logged in to WordPress for as long as you need, even forever if it makes sense for your website. This is the method I prefer for my own websites.
Here’s the special code that keeps you logged in to the WordPress Admin Area. You can put this code in your theme’s functions file or create a simple custom plugin. There’s a guide that explains how to do both.
function diwakar_academy_stay_logged_in($expires) { return 172800; // default 48 hours } add_filter('auth_cookie_expiration', 'diwakar_academy_stay_logged_in');
This code works by connecting to something called “auth_cookie_expiration” and adjusting how long your login lasts (measured in seconds). Normally, it’s set to 48 hours, but you can change it to any number you prefer.
If you want to stay logged in indefinitely, just use a really big number like 3,153,600,000 to stay logged in for 100 years! To help with these conversions, you can use a free time conversion calculator.
WordPress gives us some handy time options that we can use instead of writing out long numbers in seconds.
MINUTE_IN_SECONDS
HOUR_IN_SECONDS
DAY_IN_SECONDS
WEEK_IN_SECONDS
MONTH_IN_SECONDS
YEAR_IN_SECONDS
So instead of writing out a long number in seconds, we can use something like “3 * HOUR_IN_SECONDS” to mean a time period of 3 hours.
Here’s a little extra tip! If you want the “Remember Me” box to be checked automatically on the WP Login Page, you can add this code either through your theme functions or a custom plugin.
function diwakar_custom_login_checkbox() { ?> <script> document.getElementById('rememberme').checked = true; document.getElementById('user_login').focus(); </script> <?php } add_filter('login_footer', 'diwakar_custom_login_checkbox');
You don’t have to make any other changes, just add this code and you’re good to go. Once it’s in place, the “Remember Me” checkbox will be automatically selected by default.
Install a plugin
If you want to make your login last longer than 14 days without dealing with custom code, using a plugin is the way to do it. Right now, there are only a couple of good plugins available in the WP Plugin Directory for this purpose.
Related Articles
Leave a Reply