Create different cache files with dynamic and mandatory cookies

Some plugins may use cookies to display dynamic page content based on conditions related to the current web page visitor. For example, a third-party plugin may set a cookie related to your country in order to show content in your specific language.

There's no issue with this for non-cached page views where PHP code runs to apply the dynamic content correctly, but problems can occur for cached page views where only the static HTML is served.

Even if the cookie is set correctly, the content displays exactly as it did during the page view that created the cache. So if the page was cached in English, it means visitors from other countries would also see the page in English.

Ideally, the third-party plugin would use JavaScript to manage this since it will run on a cached page, but when that's not possible, there are 2 useful filters in WP Rocket that developers can use.

Filters for developers

If you are the plugin developer, you can use these in your code to create compatibility with our plugin. They can be used simultaneously if necessary, or either can be used alone depending on your circumstances.

  • rocket_cache_dynamic_cookies
    This creates a cache file for each value of a specified cookie.
  • rocket_cache_mandatory_cookies
    This prevents caching until the specified cookie is set

An example of how these filters work can be seen in our compatibility with the Aelia Currency Switcher plugin.

This produces cache files like the following, with the cookie value added to the filename:

Helper plugin

If your site uses a plugin that relies on cookies to display dynamic content, or if you need to delay serving cached pages until a specific cookie is set, you can ensure compatibility with WP Rocket by modifying the helper plugin shown below.

📥 Download (.zip): WP Rocket | Set Dynamic and Mandatory Cookies
Developers: You can find the code for this plugin on GitHub.

How to use it

1. Download the helper plugin, unzip it, and open the PHP file in a text editor.

2. You need to identify the name of the cookie used by your plugin; you can then define if you want to define it as a mandatory cookie (required before serving cached pages) or a dynamic cookie (used to display personalized or dynamic information). Or both. 

In the screenshot below, the cookie name is: yith_woocompare_list

cookies list

3. If you want to define it as a mandatory cookie, place the cookie name in line 27 of the plugin.

Change this:
$cookies[] = 'mandatory_cookie';

To this:
$cookies[] = 'yith_woocompare_list';

Please note: The cache won't be delivered until the mandatory cookie is set in the visitor's browser.

4. If you want to define it as a  dynamic cookie, place the cookie name in line 40 of the plugin.

Change this:
$cookies[] = 'dynamic_cookie';

To this:
$cookies[] = 'yith_woocompare_list';

Note you can add multiple cookies there by duplicating the line.

5. Zip up the plugin and install/activate it on your site.

Possible browser cache issue

If you encounter a problem where the browser shows the wrong content to the user after the cookie changes, please install and activate the following helper plugin to adjust the browser caching rules:

📥 Download (.zip): WP Rocket | Add custom htaccess rules to set no-store on HTML
Developers: You can find the code for this plugin on GitHub.

The first visit is not getting the cached page

If a mandatory cookie is set by your theme or a plugin you have installed, no cache will be served until the cookie is set on the visitor’s browser.

For example, if a mandatory cookie is set after the first page load (an example of this is the Polylang's Detect browser language feature), then the cached file(s) will be served once the user navigates to a different page or refreshes the page. In that case, visitors of the site will always get a non-cached page when they visit the site for the first time (or after they clear the cookies on their browser).

Plugins with automatic compatibility

In the following articles, you can find the list of plugins with automatic compatibility based on mandatory and dynamic cookies:

Additional layers of cache

WP Rocket's dynamic and mandatory cookies features are not compatible with additional layers of cache. This applies to services such as Cloudflare APO, Cloudflare Cache Everything, Varnish, etc.

Here is more information on why an additional cache layers are not compatible:

  • They cannot exclude specific pages from cache when mandatory cookies are not present (mandatory cookies handling). They cannot serve different cached versions based on cookie values required by dynamic cookies (dynamic cookies handling).
  • Since additional cache layers respond before WP Rocket's own caching logic can run, the appropriate handling for each type of cookie behavior is required to properly deliver dynamic content based on cookies.
Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.