Overriding DONOTCACHEPAGE via Filter

In this article

Introduction

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:

1
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);
2
This tells WordPress to load wp-content/advanced-cache.php.
3
From inside of advanced-cache.php WP Rocket tells WordPress to load wp-rocket/inc/front/process.php.
4
From inside of process.php WP Rocket loads wp-content/wp-rocket-config/{site-url.tld}.php which contains relevant user-defined configurations.
5
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 );

You can copy the code snippet above into a custom (MU) plugin like this:

Custom MU Plugin

Follow the general instructions in this article:
How to Make a Custom (MU) Plugin

1
Create a folder named mu-plugins in your wp-content folder.
In case a mu-plugins folder already exists, don’t create a new one, just use the existing one.
2
Using a basic text editor create a new file and put the following code in it:
<?php
defined( 'ABSPATH' ) or die( 'Cheatin&#8217; uh?' );
/**
 * Plugin Name: WP Rocket | Force Caching
 * Description: Overrides the DONOTCACHEPAGE constant in order to force caching.
 * Author:      WP Rocket Support Team
 * Author URI:  http://wp-rocket.me/
 * License:     GNU General Public License v3 or later
 * License URI: http://www.gnu.org/licenses/gpl-3.0.html
 */

add_filter( 'rocket_override_donotcachepage', '__return_true', PHP_INT_MAX );
	
3
Save the file as wp-rocket-force-caching.php
4
Upload this file to the mu-plugins folder created in step 1.
5
Finally, make sure to deactivate and re-activate WP Rocket.

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 Logged-in User Cache option in WP Rocket in order to provide fast-loading, cached pages for your loggend-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 Logged-in User Cache option to work properly.

In order to do so, you’d best create a custom plugin or MU plugin as described above.