Skip to content

Cookie Banner

Ben Gillbanks edited this page Sep 12, 2019 · 10 revisions

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.

Close, Accept, Decline

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'.

Actions

toolbelt_cookies_accepted

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.

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' );

Filters

toolbelt_cookie_message

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' );

toolbelt_cookie_button_text

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' );

CSS Customizations

Adjust the font size

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; }

Google Analytics Tracking

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 in the cookie banner. It will ignore anybody who does not accept the cookie banner.

You can’t perform that action at this time.