nocache_headers

Home » Functions » nocache_headers

Function Name: nocache_headers

In WordPress, the nocache_headers function is a handy utility that helps to set HTTP headers to prevent caching of a web page. By calling this function, developers can ensure that the page content is always fresh and updated for visitors, rather than being served from a cached version.

The nocache_headers function specifically sets the following headers:

  1. "Cache-Control": This header specifies the caching directives that apply to the web page. By setting it to "no-store, no-cache, must-revalidate, max-age=0", the function instructs the browser and any intermediate caching systems to completely bypass caching for the page.

  2. "Expires": This header indicates the expiration date and time of the page content. By setting it to a date in the past, such as "Thu, 01 Jan 1970 00:00:00 GMT", the function ensures that the page content is considered expired immediately.

  3. "Pragma": This header is included for compatibility with older HTTP/1.0 caching systems. By setting it to "no-cache", the function instructs these systems to not cache the page.

The nocache_headers function is typically used in scenarios where dynamic content is served, such as login pages, shopping carts, or any other page that should not be cached. By preventing caching, it ensures that users always see the most up-to-date version of the page and avoids potential issues with stale content.

Example Usage:

<?php
// Include the WordPress core file
require_once('wp-load.php');

// Call the nocache_headers function to prevent caching
nocache_headers();
?>

In the above example, we first include the WordPress core file using require_once to ensure that the necessary functions are available. Then, we simply call the nocache_headers function, which sets the appropriate HTTP headers to prevent caching for the current page. This ensures that any subsequent requests for the page will always fetch the latest content from the server.

Remember to use the nocache_headers function judiciously and only when necessary, as preventing caching can impact the performance of your website.

Learn More on WordPress.org

WordPress snippets using the nocache_headers function

Register an account to save your snippets or go Pro to get more features.