Created
July 7, 2017 11:52
-
-
Save jamesmorrison/04a2fd8f8936c9fe14a3e682ece0e366 to your computer and use it in GitHub Desktop.
Set a max age for WP pages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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