This article describes the advanced JavaScript customization options of the Cookie Banner plugin.
JavaScript events
The Cookie Banner plugin will fire several JavaScript events. They are listed below.
Actions
wps-cb:show
This event (on document) can be sent to show the consent popup. It will pre-select whatever preference the user has selected before, if any.
document.dispatchEvent(new CustomEvent('wps-cb:show'));
wps-cb:hide
This event (on document) can be sent to hide the consent popup. It will not handle any preference saving by itself; this is handled by the popup buttons.
document.dispatchEvent(new CustomEvent('wps-cb:show'));
Events
wps-cb:consent
This event (on document) is sent whenever the user saves their preferences (via the ‘save’ or ‘accept all’ buttons) or on page load when the banner was shown before.
The event has a single argument, detail, which contains an array of the internal ID of each category selected or selected previously.
document.addEventListener('wps-cb:consent', function(event) { if(!event.detail || !Array.isArray(event.detail)) return; console.log(event.detail); // e.g. ['functional', 'marketing'] });
JavaScript objects
The Cookie Banner plugin exposes some data about its state.
WPSaurus.CookieBanner.consented
This property contains an array of the currently accepted internal IDs.