Using PHP Headers to create custom cache exclusions

1 min read

To offer more flexibility in the way you handle cache exclusions, you can also add a custom PHP header inside of WordPress to exclude pages as you see fit.

Nginx Caching

This will work for both FastCGI and Redis Page caching on GridPane servers.

Out of the box, both of our caching Nginx configurations contain the following:


fastcgi_cache_bypass $skip_cache$upstream_http_do_not_cache;
fastcgi_no_cache $skip_cache$upstream_http_do_not_cache;


srcache_fetch_skip $skip_cache$upstream_http_do_not_cache;
srcache_store_skip $skip_cache$upstream_http_do_not_cache;

What These Do

The above will ensure that if a page has the following HTTP response header, it will result in a cache MISS: 

do-not-cache: true

Add a HTTP Response Header via PHP in WordPress

Setting the header via PHP looks as follows:

<?php header("do-not-cache: true"); ?>

In WordPress, you can use the send_headers hook to add your headers.

For example, adding this to your themes functions.php would create an exclusion across your entire site:

function my_headers() {
header( 'do-not-cache: true' );
add_action( 'send_headers', 'my_headers' );

You can use the above to create add the do-not-cache header across your site as you see fit, such as to custom page templates and so forth.