Recommended location to set response headers?

I’m looking to set an Expires response header for all page requests. I see the doctype in the theme’s header.php file, so I’m guessing I could set it right before that line, but I’m wondering if there is a better or more appropriate/recommended place to do this?

<?php
/** File: /themes/[theme]/header.php
 * The header for our theme.
 *
 * Displays all of the <head> section and everything up till <div id="content">
 *
 * @package goorin
 */
// Should I set the Expires header here?
header('Expires: ' . gmdate($somedatetime) . ' GMT');
?><!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
......

1
1

There’s a filter for that:

function wpse_203745_wp_headers( $headers ) {
    $headers['Expires'] = gmdate( $somedate ) . ' GMT';
    return $headers;
}

add_filter( 'wp_headers', 'wpse_203745_wp_headers' );

Leave a Comment