Create different cache files with dynamic and mandatory cookies

Some plugins may be using cookies to set dynamic information about a user and differentiate content accordingly. 
For example, a third party plugin may set a cookie to detect and store your country, in order to show you content in your specific language. A problem can occur with caching if the plugin is trying to dynamically change the content using PHP.  PHP does not run on a cached page, so even if the cookie is set correctly, the content won't change and this will result in incorrect content being displayed.

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.

In this article

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 circumstance.

  • 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 plugins for customers

Dynamic cookies

If you are the user of a plugin that uses cookies for dynamic information, you can create compatibility with WP Rocket on your site by modifying the helper plugin below. 

📥   Download (.zip):  WP Rocket | Cache Dynamic Cookie
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 know the name of the cookie storing the dynamic information. You can find this in the developer tools of your browser. If you're not sure how to do this, contact the plugin developer and ask them :)

In the screenshot below the cookie name is: yith_woocompare_list

3. Place the cookie name in line 21 of the plugin.

Change this line:

define( 'WPROCKETHELPERS_CACHE_DYNAMIC_COOKIE', 'your-cookie-id-here' );

To:

define( 'WPROCKETHELPERS_CACHE_DYNAMIC_COOKIE', 'yith_woocompare_list' );

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

Mandatory cookies

If you need to prevent the delivery of the cached pages until a cookie is set, you can use the following helper plugin. 

📥   Download (.zip):  WP Rocket | Cache Mandatory Cookie
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. Similarly as for the Dynamic Cookies example, you need to know the name of the cookie.

3. Replace the placeholder value your-cookie-name-here in line 29 with the cookie name you want to use. You can add more cookies as needed, by duplicating that line.

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

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

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.

Why is the first visit not getting the cached page?

If a required/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).

Will this method work with server-based cache and Varnish?

No. Using these features requires PHP to run. When there are additional layers of cache and pages are served from there, no PHP will run.

Therefore these features are not compatible with additional layers of cache.

WP Rocket has integrated automatic compatibility based on mandatory cookies with the plugins listed in this article.

Cloudflare APO

Cloudflare APO is not compatible with WP Rocket's dynamic and mandatory cookies features, because APO can't serve different cache versions based on different cookie values. You can read more in this article.

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