Alexandar Tzanov's Personal Blog

WordPress Cache-Control Headers


You might see in your WordPress Site Health report that your site is not using Cache-Control headers. You can find this under the page cache test on the Site Health page. Usually this is a result of not having an activated plug-in for managing page cache. I have also seen this being blamed on Apache’s mod_pagespeed, though not everyone us using Apache to serve their WordPress site. If you do not want or are unable to use a server-side cache plug-in, you can easily enable client caching without making changes to Apache config files or .htaccess. You can modify the headers generated by WordPress or lack there off. As of this writing WordPress (6.2.2) only generates cache control headers when you are in the admin dashboard – specifically to disable caching. Outside of the dashboard cache should be handled by a plug-in or custom solution, like the one below.

The following code will enable client cache control by making changes to the headers WordPress generates, if the user is NOT in the admin dashboard.

// Enable client caching of content.
if ( ! function_exists( 'mysite_client_cache' ) ) :
	/**
	 * @name mysite_client_cache
	 * @description Enable client caching. Disabled by default by WordPress for compatibility with third-party cache plug-ins. Will run last.
	 * @return array
	 */
	function mysite_client_cache( $headers ) {
		global $wp;

		$current_request_path = $wp->request;

		// Update headers if not viewing WP admin dashboard
		if ( '' !== $current_request_path ) {
			$current_request_path = trim( $current_request_path, '/' );

			if ( 'wp-admin' !== $current_request_path ) {
				$headers[ 'Cache-Control' ] = 'public, max-age=604800';
			}
		}
		
		return $headers;
	}
endif;

add_filter( 'wp_headers', 'mysite_client_cache', 100, 1);
  • This has been tested with WordPress 6.2.2.
  • You can place this in your currently active theme’s “functions.php” file or a custom plug-in.
  • This solution will generate the desired cache control headers for both logged-in and anonymous users.

What the above filter does?

  1. This filter will be applied at the time when WordPress generates the page headers.
  2. The filter priority is set to “100”, which is the lowest priority for a WordPress filter. This is done in an effort to ensure that our changes to the headers are made at the end of the execution of “wp_headers”. Thus, avoiding being overwritten by something else. The function in the filter will accept one input.
  3. The filter works by checking what is the page request path. Our changes will be applied if the user is not navigating through “wp-admin” – the admin dashboard.

Limitations

The code above, as is, will have the client cache any page outside the admin dashboard for one week. That includes the blog’s homepage. This will work for infrequently updates site. So, keep that in mind.

If you need less time then lower the value for “max-age”. It is in seconds. You can also go more granular and add more conditions, e.g., different cache age for the home-page vs other types of pages.

Resources

Below are couple of developer resources to get you started about the values you can use for Cache-Control and WordPress hooks.

,

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.