Created
July 24, 2014 17:09
-
-
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.
This file contains hidden or 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 | |
/** | |
* 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