Skip to content

Instantly share code, notes, and snippets.

@jamesmorrison
Created July 7, 2017 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesmorrison/04a2fd8f8936c9fe14a3e682ece0e366 to your computer and use it in GitHub Desktop.
Save jamesmorrison/04a2fd8f8936c9fe14a3e682ece0e366 to your computer and use it in GitHub Desktop.
Set a max age for WP pages
<?php
/**
* Plugin Name: Cache Controller
* Plugin URI: #
* Description: HTTP Cache Control headers for WordPress.
* Version: 0.0.1
* Author: James Morrison
* Author URI: https://james.morrison.me/
**/
// Security check
defined( 'ABSPATH' ) || die( 'Direct file access is forbidden.' );
// Send headers
add_action( 'template_redirect', function() {
if ( is_user_logged_in() || defined( 'LOCAL_DEV' ) && LOCAL_DEV || defined( 'WP_LOCAL_DEV' ) && WP_LOCAL_DEV ) {
header( "Cache-Control: no-store, no-cache, must-revalidate" );
header( "Cache-Control: post-check=0, pre-check=0", false );
header( "Expires: Fri, 30 Dec 1983 07:30:00 GMT" );
header( "Pragma: no-cache" );
header( "Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT" );
} else {
header( "Cache-Control: public, max-age=300" );
}
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment