Excluding CookieOfficer code from WordPress cache plugins
Find out how to reconcile site optimisation with the banner working correctly.
Plugins that speed up WordPress sites combine scripts into one file, defer them or delay them until the visitor's first interaction. These actions mean that automatic blocking no longer covers scripts that were launched before CookieOfficer. Below you'll find a way to check for the problem, plus the exclusions for WP Rocket, LiteSpeed Cache and Autoptimize.
Before you start
You need WordPress administrator permissions and the CookieOfficer code already in place. If the code isn't on your site yet, start with the article Setting up CookieOfficer on WordPress.
How do I check for the problem?
The symptom can be misleading, because the banner usually works. The problem only shows up in a scan.
Don't be surprised, either, that the test on the Installation page passes. It checks whether the banner works on the domain, not where the code sits. A moved or deferred script will still be detected.
- Run a scan of your site in the panel and go to the Cookies page.
- Check the message above the list of detected items. If it mentions trackers running after the visitor has rejected consent, something is starting before our code.
- Open the page source and check where our script sits. If it has been moved to the end of the document, combined with other files or given a deferring attribute, a speed-up plugin did it.
The second, rarer symptom is a banner that appears with a noticeable delay or only after the visitor moves the mouse.
What should you do?
In all plugins it is enough to exclude one string of characters: cookieofficer. It covers the address our code loads from, so you don't need to enter the full path or the project ID.
Add the exclusion in every field the plugin provides: combining, minification, deferring and delaying execution. Entering it in just one field isn't enough, because the other mechanisms will still touch our code.
You don't need to exclude the banner styles. We don't load a separate stylesheet; the appearance is built inside our script.
WP Rocket
- Go to Settings > WP Rocket > File Optimization.
- In the JavaScript Files section, find the exclusion fields. They only appear after you turn on minification, deferring or delayed execution.
- Enter
cookieofficerin the Excluded JavaScript Files field. - If you use Delay JavaScript execution, enter the same string in its exclusion field as well.
- Save the changes and clear the cache.
LiteSpeed Cache
- Go to LiteSpeed Cache > Page Optimization.
- Open the Tuning tab.
- Enter
cookieofficerin the JS Excludes field. - If you turned on deferring or delaying in the JS Settings, enter the same string in the JS Deferred / Delayed Excludes field.
- Save the changes and clear the cache.
Autoptimize and other plugins
In Autoptimize, go to Settings > Autoptimize, open the JavaScript Options section and add cookieofficer to the “Exclude scripts from Autoptimize” list.
In other plugins the principle is the same, only the field names differ. Look for settings described as combining files, minification, deferred loading or delayed script execution, and next to each of them, the exclusion fields.
Clear the cache after the change
The exclusion only takes effect on newly generated pages. As long as the cache holds a version with the code moved by the optimiser, visitors get exactly that version. Clear the plugin's cache, and if you use server-side caching or a CDN, clear those as well.
Then run the scan again and check the message on the Cookies page. It should disappear.
Common mistakes to avoid
- Don't add the exclusion in just one place. Plugins usually have several exclusion fields, one for each mechanism, and each of them can break blocking on its own.
- Don't forget to clear the cache. Without that you'll see the page from before the change and conclude that the exclusion didn't help.
- Don't be surprised by a delay in trackers after consent. If the plugin delays script execution, they will start later than usual after the visitor's decision. This doesn't break compliance, because they weren't supposed to run before consent anyway.
- Don't count on the problem going away by itself after a plugin update. The exclusions only need to be entered once, but it's worth checking them after every major change to the optimisation settings.