Overriding DONOTCACHEPAGE via Filter

In this article


Most caching plugins for WordPress use a de facto standard to exclude pages from being cached: the constant DONOTCACHEPAGE. If defined as true during a request, WP Rocket would not cache the page in question, but allow a regular database request to WordPress.

However, in some rare cases, that behavior needs to be overridden, and WP Rocket lets you do that. Here is how.

How WP Rocket Caches a Page

The ultimate goal of any caching plugin must be to avoid loading WordPress, but deliver any requested page as a collection of static files to the browser whenever possible.

The caching of a page with WP Rocket could be simplified as follows:

One of the first files WordPress loads after the initial request to its index.php is wp-config.php. First thing WP Rocket has done is to define a constant— WP_CACHE—as true at the top of wp-config.php: define( 'WP_CACHE', true);
This tells WordPress to load wp-content/advanced-cache.php.
From inside of advanced-cache.php WP Rocket tells WordPress to load wp-rocket/inc/front/process.php.
From inside of process.php WP Rocket loads wp-content/wp-rocket-config/{site-url.tld}.php which contains relevant user-defined configurations.
Still from inside of process.php, and after having processed a bunch of checks for exclusions etc., WP Rocket then caches the files for the page requested.

Once the page has been cached, the request will directly be served from the cache, without any database query.

Excluding a Page From Cache via DONOTCACHEPAGE

For some types of pages it is imperative to not be cached. Think of an e-commerce scenario: when a customer enters checkout, they wouldn’t want to see a cached page with some previous customer’s payment data.

Elaborate plugins like WooCommerce (and many others) use the DONOTCACHEPAGE constant to let caching plugins know about certain pages or endpoints that should not be cached in any case.

Accordingly, all popular caching plugins, including WP Rocket, support the constant and would not cache a request for which DONOTCACHEPAGE is defined as true.

Overriding DONOTCACHEPAGE via Filter in WP Rocket

As handy as a commonly used constant like DONOTCACHEPAGE can come in, there may be cases where a site admin needs to override it in order to still get a specific request cached, no matter whether a third-party plugin has tried to employ the common constant for cache exclusion of a page.

WP Rocket has you covered in that case. All you need to do is pass true to a special filter in WP Rocket, and WP Rocket will ignore the DONOTCACHEPAGE constant.

add_filter( 'rocket_override_donotcachepage', '__return_true', PHP_INT_MAX );

Helper Plugin

The following helper plugin will add the above code, to easily override a DONOTCACHEPAGE constant.

📥   Download (.zip):  WP Rocket | Force Page Caching

Developers: You can find the code for this plugin on GitHub.

Heads up! Using the Force Page Caching Helper plugin will not override excluded pages in the Never Cache URL(s)Never Cache Cookies rules. or individual Never cache this page option on the pages.

Example: Cached Pages for Members

Let’s say, you use a membership plugin to serve specific content for logged-in users only. You would want to enable the User Cache option in WP Rocket in order to provide fast-loading, cached pages for your logged-in site members.

Some membership plugins define the DONOTCACHEPAGE constant for logged-in users, because a common use case would be to not serve static, but dynamic content to members.
In our example, however, you would only aim to deliver static content to your logged-in members, additional to what non-logged-in users would see. So you would need to override the DONOTCACHEPAGE directive set by the membership plugin in order for WP Rocket’s User Cache option to work properly.

In order to do so, you’d best use the helper plugin provided above.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.