nocache_headers()WP 2.0.0

Sets headers that prevent caching in all browsers.

nocache_headers() should be used before any information is output to the screen.

The function removes the HTTP header Last-Modified or sets it to an empty value. It also sets other headers related to browser page caching that are returned by the function wp_get_nocache_headers():

Expires: Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
Pragma: no-cache

This function is used throughout the WordPress admin panel (wp-admin/admin.php) and during AJAX requests (wp-admin/admin-ajax.php) to disable browser caching set in the server settings of nginx or apache (htaccess). Enabled browser caching in the admin panel will interfere with the normal operation of WP...

No Hooks.

Returns

null. Returns nothing.

Usage

nocache_headers();

Examples

1

#1 Enable caching for selective AJAX request

Suppose we need our AJAX request to be cached in the browser, and upon repeating this request within a specified time, the browser should take the data from the cache and not make an AJAX request.

This can be achieved by setting the necessary headers for caching. This can be done directly or through the filter nocache_headers.

Option: through the nocache_headers filter

The filter needs to be inserted directly into the PHP code, and in the filter, check that our request is indeed triggered, so that caching works for other requests.

Suppose our AJAX event is called my_ajax_action, then:

add_filter( 'nocache_headers', function( $headers ){

	// enable caching only for our event
	if( $_REQUEST['action'] === 'my_ajax_action' ){
		$life_time = HOUR_IN_SECONDS; // cache for one hour

		$headers = array(
			'Expires'       => gmdate( 'D, d M Y H:i:s', time() + $life_time ) .' GMT',
			'Cache-Control' => 'public',
		);
	}

	return $headers;
});

// AJAX request handler function
add_action( 'wp_ajax_'.'my_ajax_action', 'my_ajax_action_function' );
function my_ajax_action_function(){
	// do something and return the result for the AJAX request
	// the result of this request will be cached in the browser!
}

The advantage of this option:
Caching can be set up immediately for several events by simply adding a check for the event name.

Option: directly in the code

Directly in the code is often more convenient because headers can be set right in the AJAX request handler function.

Suppose our AJAX event is called my_ajax_action, then:

// AJAX request handler function
add_action( 'wp_ajax_'.'my_ajax_action', 'my_ajax_action_function' );
function my_ajax_action_function(){

	// enable caching in the browser
	$life_time = HOUR_IN_SECONDS; // cache for one hour
	header( 'Expires: ' . gmdate('D, d M Y H:i:s', time() + $life_time) . ' GMT' );
	header( 'Cache-control: public' );

	// do something and return the result for the AJAX request
	// the result of this request will be cached in the browser!
}

The advantage of this option:
Less code and caching can be set up right in the handler function.

A huge downside of such caching is that you cannot programmatically invalidate this cache. If the browser has cached it, then it's the end. Suppose you made a mistake and set the cache for 3 years. The client will never figure out how to clear the cache. The only way is to change the request parameters so that the request becomes different for the browser.

0

#2 Cancel caching in the browser

Cancel page caching before executing any code related to sessions or that outputs information that should always be up-to-date:

// set the headers overriding the browser cache
nocache_headers();

// execute the code
0

#3 Let's add our own nocache headers to the standard ones

Let's add our additional headers for all calls to the function wp_get_nocache_headers(). To do this, we use the hook nocache_headers:

add_filter( 'nocache_headers', function( $headers ) {

	return [
		'Cache-Control' => 'no-store, no-cache, must-revalidate, max-age=0, some-custom-thing',
		'Pragma'        => 'no-cache',
		'Expires'       => gmdate( 'D, d M Y H:i:s \G\M\T', time() )
	] + $headers;
} );

The function is called, for example, for all administration pages.

Notes

Changelog

Since 2.0.0 Introduced.

nocache_headers() code WP 6.8.1

function nocache_headers() {
	if ( headers_sent() ) {
		return;
	}

	$headers = wp_get_nocache_headers();

	unset( $headers['Last-Modified'] );

	header_remove( 'Last-Modified' );

	foreach ( $headers as $name => $field_value ) {
		header( "{$name}: {$field_value}" );
	}
}