5

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>
......
10

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' );
  • Close to 20k … upvoting. – kaiser Sep 25 '15 at 18:35
  • Thanks. Is it appropriate to add this code to /themes/[theme]/header.php? Sorry I'm a WP noob – Nick Rolando Sep 25 '15 at 18:40
  • 1
    No, functions.php ;) – TheDeadMedic Sep 25 '15 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.