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.
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 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.
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.
Helper plugin for 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.
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:
- Plugins with compatibility based on mandatory cookies
- Plugins with compatibility based on 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.