Using the Plugin with Caching Plugins
Lightweight Cookie Notice works with page caching plugins. In one case the caching plugin needs a setting: when you added scripts to the HTML Head or HTML Body fields of your categories. This article explains why and gives the procedure for the most used caching plugins.
Do I need to configure anything?
| Your setup | Action needed |
|---|---|
| You use only the cookie notice and the cookie settings window, with no scripts in the categories | None. |
| You added scripts to the HTML Head or HTML Body fields of one or more categories | Follow the procedure for your caching plugin below. |
| You do not use a page caching plugin or server page cache | None. |
Why a setting is needed
The cookie notice itself is generated in the browser with JavaScript. The plugin checks in the browser whether the visitor has already made a choice, so the notice is displayed correctly also when the page comes from a cache.
The scripts of the categories work differently. They are added to the page on the server, when the page is generated, based on the cookies of the visitor who requests it. A page cache saves one copy of the page and serves it to everyone. Without the right setting, two problems can occur:
- The cached copy was generated for a visitor who accepted. It contains the scripts and is served also to visitors who have not accepted.
- The cached copy was generated for a visitor who had not accepted. It does not contain the scripts and is served also to visitors who have accepted.
The solution is to tell the caching plugin to handle visitors who have the plugin cookies separately. The names of the cookies are:
| Cookie | Free | Pro |
|---|---|---|
| Acceptance | daextlwcnf-accepted | daextlwcn-accepted |
| Category states | daextlwcnf-category-status | daextlwcn-category-status |
| Encrypted key | daextlwcnf-encrypted-key | daextlwcn-encrypted-key |
WP Super Cache
No action is required. The plugin registers its acceptance cookie with WP Super Cache automatically.
In the Pro version this integration is controlled by WP Super Cache Integration in Options → Advanced → Behavior. It is on by default. Keep it on if you use the HTML Head or HTML Body fields of the categories.
W3 Total Cache
- In Cookie Notice → Options → Advanced → Behavior, turn on Page Fragment Caching Exception (W3TC) and click Save settings.
- In W3 Total Cache, open General Settings and set Page Cache Method to Disk: Basic.
- In W3 Total Cache, open Page Cache, go to the Advanced section, and turn on Late initialization.
- Save the settings of W3 Total Cache.
- Clear the cache.
With this configuration the scripts of the categories are added with the page fragment caching feature of W3 Total Cache, which keeps that part of the page out of the cache.
Note: turn on Page Fragment Caching Exception (W3TC) only together with the W3 Total Cache settings above. If the option is on and W3 Total Cache is not configured as described, text such as daextlwcn_print_scripts(false); can appear at the top of your pages. In that case complete the configuration or turn the option off.
LiteSpeed Cache
- In LiteSpeed Cache, open the Cache settings.
- Add
daextlwcnto the Login Cookie option. - Save the settings.
- Clear the cache.
WP Fastest Cache
- In WP Fastest Cache, open the Exclude tab.
- Add a new rule for cookies, with Contain selected and the value
daextlwcn. - Save the rule.
- Clear the cache.
SG Optimizer (SiteGround)
Exclude the three plugin cookies listed in the table above at the server level. Contact the SiteGround support if you need help with this exclusion.
Other caching plugins and server caches
Look for a setting that bypasses the cache, or creates a separate cache, for visitors who have a specific cookie. It is often called “never cache cookies”, “cache exclusion by cookie”, or “vary cookie”. Enter the name of the acceptance cookie of your version (see the table above).
If your caching system does not have such a setting, add your scripts with a method that does not depend on the server:
- Pro: use the Accept Cookies Script field to load the script with JavaScript at the moment of acceptance, or use Google Consent Mode. See Running Scripts Based on Visitor Consent and Setting Up Google Consent Mode.
JavaScript optimization
Some optimization plugins defer, delay, or combine JavaScript files. If the notice is displayed late (for example only after the first scroll or click) or is not displayed, exclude the plugin file from these features:
| Version | File |
|---|---|
| Free | /wp-content/plugins/lightweight-cookie-notice-free/public/assets/js/production/general.js |
| Pro | /wp-content/plugins/lightweight-cookie-notice/public/assets/js/production/general.js |
Also exclude the inline script that contains daextLwcnCookieNotice, if your optimization plugin lets you exclude inline scripts.
The plugin’s own cache: Transient Expiration
The plugin can save the data of the notice in a WordPress transient to avoid reading the options, sections, categories, and cookies from the database at every page load.
- Transient Expiration in Options → Advanced → Behavior sets the duration in seconds. With 0, the default, the transient is not used.
- While a transient is active, the changes you make to the options, sections, categories, and cookies are not visible on the front end until the transient expires.
- To apply your changes immediately, go to Cookie Notice → Maintenance, select Delete Transient, and click Execute Task.
After every change
When you change the options of the plugin or of your caching plugin, clear the cache of the caching plugin and of your browser before you test.
Test with caching enabled
- Open the site in a private browser window. Do not click the notice. View the page source and check that the scripts of your categories are not present.
- Accept the cookies and open another page. View the source and check that the scripts are present.
- Open the site in a second private window (a different browser is better). Do not click the notice. Check again that the scripts are not present.
Step 3 is the important one: it confirms that a page generated for a visitor who accepted is not served to a new visitor.