WordPress Plugin providing a "accept" banner before embedding Google Analytics (and other scripts that can be registered)
This plugin has a setting page under "Tools". There, you can set some common Analytics codes, and disable the banner altogether if you only use the Gutenberg part of it.
You can use, for example,
if (function_exists('wpca_enqueue_script')){
wpca_enqueue_script($id, $url);
} else {
wp_enqueue_script($id, $url);
}
to enqueue a script that should be loaded in case the cookies are accepted.
Do this wherever (i.e., in a init hook) you would do the wp_enqueue_script
.
This plugin exposes the following global functions:
wpcaCookiesAreAllowed(); // returns whether all cookies are allowed
wpcaAcceptCookies(); // trigger "accept all cookies"
wpcaContentTypeIsAllowed(contentType); // returns whether the given content type is allowed
wpcaAcceptContentType(contentType); // trigger "accept the given content type"
Additionally, the event wpcaCookiesAccepted
is triggered when all cookies are accepted,
and wpcaContentTypeAccepted
is triggered if a certain subset of cookies for one content type is accepted.
This plugin provides a Gutenberg block, called "External Content Wrapper", which blocks the child blocks from being rendered until sufficient cookie permissions are given.
- Add the "External Content Wrapper" block to your post/page
- Select the content type (Analytics, Marketing, Social Media, etc.)
- Add your content blocks inside the wrapper
- The content will only be displayed after the user accepts the corresponding cookie type
This is particularly useful for embedding content from external sources like YouTube videos, Google Maps, or social media feeds that may track user data.