For some reason the auth_cookie_expiration filter doesn’t work on my site… if someone has the same problem here I share the function I used where all the parameters can be customized:
function set_auth_cookie() {
if (is_user_logged_in()) {
$user_id = get_current_user_id();
$settingValue = $_COOKIE['wp-settings-' . $user_id];
$settingTimeValue = $_COOKIE['wp-settings-time-' . $user_id];
wp_clear_auth_cookie();
$auth_cookie_name = SECURE_AUTH_COOKIE;
$scheme = 'secure_auth';
$expiration = time() + YEAR_IN_SECONDS;
$manager = WP_Session_Tokens::get_instance( $user_id );
$token = $manager->create( $expiration );
$logged_in_cookie = wp_generate_auth_cookie( $user_id, $expiration, 'logged_in', $token );
$auth_cookie = wp_generate_auth_cookie( $user_id, $expiration, $scheme, $token );
$secure = is_ssl();
$secure_logged_in_cookie = $secure && 'https' === parse_url( get_option( 'home' ), PHP_URL_SCHEME );
setcookie( $auth_cookie_name, $auth_cookie, array('expires' => $expiration,'path' => PLUGINS_COOKIE_PATH,'secure' => $secure,'httponly' => true,'samesite' => 'Strict') );
setcookie( $auth_cookie_name, $auth_cookie, array('expires' => $expiration,'path' => ADMIN_COOKIE_PATH,'secure' => $secure,'httponly' => true,'samesite' => 'Strict') );
setcookie( LOGGED_IN_COOKIE, $logged_in_cookie, array('expires' => $expiration,'path' => COOKIEPATH,'secure' => $secure_logged_in_cookie,'httponly' => true,'samesite' => 'Strict') );
if ( COOKIEPATH != SITECOOKIEPATH ) {
setcookie( LOGGED_IN_COOKIE, $logged_in_cookie, array('expires' => $expiration,'path' => SITECOOKIEPATH,'secure' => $secure_logged_in_cookie,'httponly' => true,'samesite' => 'Strict') );
}
setcookie( 'wp-settings-' . $user_id, $settingValue, array('expires' => $expiration,'path' => SITECOOKIEPATH,'secure' => $secure,'httponly' => true,'samesite' => 'Strict') );
setcookie( 'wp-settings-time-' . $user_id,$settingTimeValue, array('expires' => $expiration,'path' => SITECOOKIEPATH,'secure' => $secure,'httponly' => true,'samesite' => 'Strict') );
}
}
add_action( 'init', 'set_auth_cookie' );