nocache_headers()
Set the headers to prevent caching for the different browsers.
Different browsers support different nocache headers, so several headers must be sent so that all of them get the point that no caching should occur.
No Hooks.
Return
null
. Nothing (null).
Usage
nocache_headers();
Examples
#1 Cancel caching in the browser [auto-translate]
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
#2 Enable caching for selective AJAX request [auto-translate]
Suppose we need our AJAX request cached in the browser and when this request is repeated for a specified period of time, the browser takes data from the cache and does not make an AJAX request.
This can be accomplished by setting the right headers for caching. This can be done either directly or via the [nocache_headers] filter (/hook/nocache_headers).
Option: through the nocache_headers filter
You need to insert the filter directly into the PHP code and check in the filter that it works for our query, so that caching works for other queries.
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 to the AJAX request // the result of this request will be cached in the browser! }
The upside of this option:
You can set caching for several events at once by simply adding a name check to the event.
Option: tense in the code
Directly in the code is often more convenient, because the headers can be set directly 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 upside of this option:
Less code and setting caching can be done directly in the handler function.
A huge disadvantage of such caching is that you cannot programmatically delete this cache. If the browser has cached it, that's the end. Suppose you made a mistake and made a cache for 3 years. The client will never guess how to clear the cache. The only way is to change the request parameters so that the request is different for the browser.
#3 Append our own nocache headers to default one
Let's add custom headers for all calls of wp_get_nocache_headers() function. It calls, for example, for all admin pages. We use nocache_headers hook to do this work:
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; } );
Notes
Changelog
Since 2.0.0 | Introduced. |
nocache_headers() nocache headers code WP 6.6.2
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}" ); } }