Create Different Cache Files with Dynamic and Mandatory Cookies

In this Guide

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.

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 plugin for customers

If you are the user of a plugin which 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.

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