Extend user logged in session
Hi, I get questions from a client about extending the user logged in session, because he needs to login a lot. Is there a way to do this maybe with FluentAuth?
Jonathan GwyerΒ no option without an extra plugin?
Natascha VantuykomΒ if you want to do it without a plugin, you'll want to look at https://developer.wordpress.org/reference/hooks/auth_cookie_expiration/
Natascha Vantuykom If you are trying to achieve this without a plugin, you can use the snippet below and see it for yourself:
// Complete solution: Extend login sessions to 6 months for ALL users (new and existing)
// 1. Extend all NEW logins to 6 months
function extend_login_session_duration($expiration, $user_id, $remember) {
// 6 months in seconds (6 * 30 * 24 * 60 * 60)
$six_months = 6 * 30 * 24 * 60 * 60;
return time() + $six_months;
}
add_filter('auth_cookie_expiration', 'extend_login_session_duration', 10, 3);
// 2. Extend EXISTING logged-in users to 6 months (one-time conversion)
function extend_existing_user_sessions() {
if (is_user_logged_in()) {
$user_id = get_current_user_id();
$already_extended = get_user_meta($user_id, 'session_extended_6months', true);
// Only extend once per user
if (!$already_extended) {
// Clear current short session and set 6-month session
wp_clear_auth_cookie();
wp_set_auth_cookie($user_id, true, is_ssl());
// Mark as processed
update_user_meta($user_id, 'session_extended_6months', time());
}
}
}
add_action('wp_loaded', 'extend_existing_user_sessions');