-
Notifications
You must be signed in to change notification settings - Fork 12
Cookie Banner
Toolbelt adds a simple cookie banner to your site.
This will show for visitors until they press the small cross dismissing the banner. It will then use a cookie that lasts for 365 days to hide the banner.
If you have setup a privacy policy page in your site settings then the Cookie Banner module will link to it.
The point of the basic cookie banner is to make your site compliant with the EU cookie law. However over the years this law has been refined and largely replaced by GDPR. As such, to make your site fully compliant, you will need to add full support for tracking cookies.
In a ruling on October 1st 2019 the Court of Justice of the European Union said:
consent must be specific so that the fact that a user selects the button to participate in a promotional lottery is not sufficient for it to be concluded that the user validly gave his or her consent to the storage of cookies.
There's more information below on how to add full support for the GDPR. I also included an example showing how to use Google Analytics.
I am not a lawyer. The following are things I would suggest will make your site compliant but if you're not sure it's best to get advice from someone who understands these things properly.
- Do not store cookies for user data without explicit consent.
- Minimise services that make use of cookies.
- Use a GDPR compliant Analytics/ Stats service.
By default the banner displays a close button to close the banner, this simply dismisses the banner. If the toolbelt_cookies_accepted
action has functions assigned then the buttons will switch automatically to 'accept' and 'decline'.
The toolbelt_cookies_accepted
action should contain javascript code that will be executed when the cookie banner is accepted. If the banner is not accepted it will be ignored.
Note: To be fully compliant with the GDPR you will need to make sure ALL scripts that set cookies are loaded/ executed through this function. If you set cookies without user consent then you may be in breach of the law.
function my_cookies_accepted() {
// Output javascript. The script tags will be added through the plugin so don't add them.
?>
console.log( 'cookies accepted' );
<?php
}
add_action( 'toolbelt_cookies_accepted', 'my_cookies_accepted' );
You can filter the message with the filter toolbelt_cookie_message
.
function my_cookie_banner_message( $message ) {
return 'My site uses cookies!';
}
add_filter( 'toolbelt_cookie_message', 'my_cookie_banner_message' );
If you wanted to add a custom link you could use:
function my_cookie_banner_message( $message ) {
return 'My site uses cookies <a href="https://my-site.com/cookies">Read More</a>!';
}
add_filter( 'toolbelt_cookie_message', 'my_cookie_banner_message' );
With this filter you can change the text used on the accept, decline, and close buttons.
function my_cookie_button_text( $text ) {
$text['accept'] = 'Yes please';
$text['decline'] = 'Nope';
$text['close'] = 'Close';
return $text;
}
add_filter( 'toolbelt_cookie_button_text', 'my_cookie_button_text' );
The cookie bar font size inherits the themes base font size. If this is too big/ small then it can be easily tweaked with the following CSS:
.toolbelt_cookie_wrapper { font-size: 18px; }
Since Google Analytics uses identifying cookies you should not track users until they consent to be tracked. This means they need to accept the cookies, before adding Google Analytics (GA) to the page.
Toolbelt has support for a callback function that executes when the cookie banner has been accepted. You can add code to this with the toolbelt_cookies_accepted
action. This will be called dynamically and is not affected by page caching plugins.
To add Google Analytics tracking that is GDPR compliant (I am not a lawyer, but I think this is ok), you should do the following.
- Remove any Google Analytics plugins or scripts you are currently using.
- Enable the Cookie Banner Module.
- Customize the script below with your own UA id, and then add it to your theme, or a custom plugin.
function my_cookie_script() {
?>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push( arguments ); }
var google_analytics = document.createElement( 'script' );
google_analytics.src = 'https://www.googletagmanager.com/gtag/js?id=UA-XXXX-Y';
document.head.appendChild( google_analytics );
google_analytics.onload = function() {
gtag( 'js', new Date() );
gtag( 'config', 'UA-XXXX-Y' );
};
<?php
}
add_action( 'toolbelt_cookies_accepted', 'my_cookie_script' );
This script will be executed for anyone who presses the 'accept' button on the cookie banner, and on any subsequent page load. It will ignore anybody who does not accept the cookie banner.
Toolbelt is built by Ben from Pro Theme Design.