Skip to content

Instantly share code, notes, and snippets.

@PeterBooker
Created July 24, 2014 17:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PeterBooker/b7bbff70fcc709b2818b to your computer and use it in GitHub Desktop.
Save PeterBooker/b7bbff70fcc709b2818b to your computer and use it in GitHub Desktop.
Make WordPress fully comptible with AWS CloudFront, to use as a full-page caching solution.
<?php
/**
* Add Last-Modified and Cache-Control Headers to WP Responses
*/
function wp_cloudfront_filter_headers( $headers ) {
/*
* WP already sets Cache-Control headers to avoid caching in the admin.
*/
if ( is_admin() ) {
return $headers;
}
/*
* Set Last Modified header to current time
* Makes it easier to see when the cache was last refreshed
*/
$modified = date( 'D, j M Y H:i:s e', time() );
$headers['Last-Modified'] = $modified;
/*
* Tell CloudFront not to cache certain responses.
*/
if ( is_user_logged_in() || is_404() || is_preview() ) {
$headers['Cache-Control'] = 'no-cache, must-revalidate, max-age=0, proxy-revalidate';
return $headers;
}
/*
* Set default TTL if not already set
* If none set CloudFront default is 24 hours
*/
$ttl = 5 * MINUTE_IN_SECONDS;
$headers['Cache-Control'] = 'max-age=' . $ttl . ', public';
return $headers;
}
add_filter( 'wp_headers', 'wp_cloudfront_filter_headers' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment