Skip to content

Instantly share code, notes, and snippets.

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 thegulshankumar/4964818ab641732ef86e890329307163 to your computer and use it in GitHub Desktop.
Save thegulshankumar/4964818ab641732ef86e890329307163 to your computer and use it in GitHub Desktop.
Bypass CDN Caching using functions.php
<?php // Exclude this opening tag.
/**
* The template_redirect action hook allows pre-template actions.
* We set s-maxage to 0 to ensure CDNs fetch fresh content,
* assuming the CDN respects existing headers.
*/
add_action('template_redirect', function () {
header('cache-control: s-maxage=0');
});
@thegulshankumar
Copy link
Author

Use Case

Imagine managing two WordPress sites: example.com and example.com/wp/, both are hosted on Cloudways with the Cloudflare Enterprise Addon.

However, an issue arises with Breeze caching. Breeze fails to purge Cloudflare's cache for the WordPress installation in the subdirectory example.com/wp/.[1] Additionally, there's no direct way to deactivate the Cloudflare HTML cache specifically for the subdirectory.

On a positive note, Cloudflare does respect existing headers and allows an opportunity to modify the caching behaviour using the s-maxage directive. By implementing this workaround, we can bypass the Cloudflare Cache for the HTML while continuing to benefit from a Page Caching plugin such as Breeze.

Reference

  1. https://wordpress.org/support/topic/doesnt-purge-cloudflare-addon-cache-for-subdirectory-based-wp/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment