How to Modify Your Cache Control Header in Pressable

Category: Speed | Last modified: August 28, 2020

By default, Pressable’s CDN will set the cache-control: max-age to 7 days — unless the asset has a parameter, in which case it will be 10 years. If you’d like to increase this amount of time, you may do so by appending any parameter to your static assets.

That parameter will increase the time the asset remains cached from 7 days to 10 years.

The following code snippet as a plugin will allow you to extend the cache control for all jpeg, jpg, gif, png, bmp, ico, js and css files.

<?php
/*
Plugin Name: Pressable CDN Extender
Plugin URI: https://yoursite.com
Description: Extend Pressable's cache-control from 7 days until 10 years for static assets
Version: 1.0
Author: YourName
Author URI: https://yoursite.com
*/

function pressablecdn_template_redirect() {
  ob_start();
  ob_start( 'pressablecdn_ob_call' );
  ob_flush();
}

function pressablecdn_ob_call( $html ) {
  $cdn_extensions = array('.jpg','.jpeg','.gif','.png','.css','.bmp','.js','.ico');
  foreach ( $cdn_extensions as $cdn_extension ) {
    $html = str_replace($cdn_extension, $cdn_extension.'?extend', $html);
  }
  return $html;
}

if( !is_admin() && strpos($_SERVER['DOCUMENT_URI'],"wp-admin") === false && strpos($_SERVER['DOCUMENT_URI'],"wp-login.php") === false ) {
  add_action( 'template_redirect', 'pressablecdn_template_redirect');
}

You can verify that the cache-control headers have changed by running the following curl commands for the static asset(s) in question:

$ curl -sI https://149345589.v2.pressablecdn.com/wp-content/uploads/2018/08/logo-new-4x-compressed.png | grep ^cache-control
cache-control: max-age=604800
$ curl -sI https://149345589.v2.pressablecdn.com/wp-content/uploads/2018/08/logo-new-4x-compressed.png?expires | grep ^cache-control
cache-control: max-age=315360000
An Automattic Invention A family of WordPress solutions.