WP Super Cache and Cookie Banners

EDIT: Updated for WP Super Cache 1.6.3 and newer.

More sites use cookie banners now that the GDPR is active but some are finding that their banners are misbehaving once they enable caching.

This is a similar issue to the one that happened to some page counter plugins in the past. The page counter wouldn’t increment.

When a cookie banner is clicked a cookie is set in the browser so the website knows this visitor has agreed to accept cookies. If the cookie is set then the cookie banner html is not sent to the browser.

I suspect the main issue is that the code that sets and checks if the cookie is set is PHP. Unfortunately because the page is cached then no PHP code is executed, and the cookie banner is displayed because it was originally cached that way.

Since WP Super Cache only knows about certain WordPress cookies it assumes everyone who doesn’t have those cookies is a first time “anonymous” visitor. It doesn’t know about your cookie banner cookie.

You have two options:

  1. Rewrite your cookie banner so it’s completely in Javascript. Do the cookie detection in Javascript and also set the cookie in Javascript. If the cookie banner has been clicked then you need to trigger an action, and other Javascript that is hooked on to that trigger will run and load the tracking cookies.
  2. Modify WP Super Cache so it knows about the cookie your cookie banner uses. Caching won’t work quite as well as before as it’ll be split between visitors who have clicked the cookie banner and those that haven’t. One cached file will display the cookie banner, and the other will not but it will have ad tracking Javascript.

Using Javascript completely is a better solution because it runs in the browser on every page load but that might not be possible every time.

Otherwise, use PHP to get WP Super Cache to play nicely with your existing code. You basically need to run do_action( 'wpsc_add_cookie', 'cookie_name' ); after init so WP Super Cache adds the cookie name to it’s cookie list. This only needs to be done once but you can leave it run all the time because it will only ever add the cookie name once. This also ensures the name is added again if the WP Super Cache configuration file is ever reset or deleted.

  1. Create a PHP script in wp-content/mu-plugins/ (or in to your own plugin).
  2. Add the cookie by using the wpsc_add_cookie action in a function that loads on “init”.
  3. Reload any uncached page and the cookie name will be added to the cookie list.
  4. Caching can only be performed by simple caching now, unless you’re willing to edit mod_rewrite rules in your .htaccess file.
/*
 * Tell WP Super Cache to cache requests with the cookie "eucookie" separately
 * from other visitors.
 */
function add_wpsc_cookie_banner() {
    do_action( 'wpsc_add_cookie', 'eucookie' );
}
add_action( 'init', 'add_wpsc_cookie_banner' );

Substitute the name of the cookie (eucookie) for your cookie name.

When your plugin is uninstalled it should remove your cookie with the following:

do_action( 'wpsc_delete_cookie', 'eucookie' );

For future reference, since cookie banners will hopefully not be around forever, here’s what they looked like in the deep, distant past of 2018. 🙂

The LA Times just gave up and don’t show anything to EU visitors.

Comments

comments

2 Replies to “WP Super Cache and Cookie Banners”

  1. #574 went in this morning but I’m also working on #580 which will allow plugin devs to add cookies to the WP Super Cache cache_key more easily.
    All the hard bits from this post will not needed 🙂

Leave a Reply