Cookiebot Shortcode

Below, you’ll find a detailed guide on how to add the Cookiebot Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Cookiebot Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Cookiebot Plugin and the shortcodes it provides:

Plugin Icon
Cookie banner plugin for WordPress – Cookiebot CMP by Usercentrics

"Cookiebot CMP by Usercentrics is a robust WordPress plugin for creating compliant cookie banners. This tool ensures your website aligns with data privacy regulations, boosting user trust."

★★★★✩ (308) Active Installs: 100000+ Tested with: 6.3.2 PHP Version: 5.6
Included Shortcodes:
  • [cookie_declaration]

Cookiebot [cookie_declaration] Shortcode

The Cookiebot shortcode is a tool that displays the user’s cookie declaration. It retrieves the Cookiebot ID (cbid) and generates a URL to fetch the declaration. The shortcode attributes include ‘lang’, which sets the language of the declaration. The ‘tag_attr’ option allows customization of the script tag attribute. If no cbid is found, a message prompts the user to add their Cookiebot ID.

Shortcode: [cookie_declaration]

Parameters

Here is a list of all possible cookie_declaration shortcode parameters and attributes:

  • cbid – Unique identifier of the Cookiebot account
  • lang – Sets the language of the Cookie Declaration
  • tag_attr – Determines the script tag attribute for the Cookie Declaration

Examples and Usage

Basic example – The basic usage of the ‘cookie_declaration’ shortcode, without any additional parameters, will display the cookie declaration in the default language set in your WordPress settings.

[cookie_declaration /]

Advanced examples

Displaying the cookie declaration in a specific language. In this example, the ‘lang’ parameter is set to ‘en’, which will display the cookie declaration in English.

[cookie_declaration lang="en" /]

Using the shortcode to control the loading of the cookie declaration script. In this example, the ‘tag_attr’ parameter is set to ‘defer’, which will defer the loading of the script until the HTML parsing is complete.

[cookie_declaration lang="en" tag_attr="defer" /]

PHP Function Code

In case you have difficulties debugging what causing issues with [cookie_declaration] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'cookie_declaration', array( static::class, 'show_declaration' ) );

Shortcode PHP function:

function show_declaration( $shortcode_attributes = array() ) {
		$cbid = Cookiebot_WP::get_cbid();

		if ( ! empty( $cbid ) ) {
			$url                  = 'https://consent.cookiebot.com/' . $cbid . '/cd.js';
			$shortcode_attributes = shortcode_atts(
				array(
					'lang' => cookiebot_get_language_from_setting(),
				),
				$shortcode_attributes,
				'cookie_declaration'
			);

			$lang = empty( $shortcode_attributes['lang'] ) ? '' : strtoupper( $shortcode_attributes['lang'] );

			if ( ! is_multisite() || get_site_option( 'cookiebot-script-tag-cd-attribute', 'custom' ) === 'custom' ) {
				$tag_attr = get_option( 'cookiebot-script-tag-cd-attribute', 'async' );
			} else {
				$tag_attr = get_site_option( 'cookiebot-script-tag-cd-attribute' );
			}

			return get_view_html(
				'frontend/shortcodes/cookie-declaration.php',
				array(
					'url'      => $url,
					'lang'     => $lang,
					'tag_attr' => $tag_attr,
				)
			);
		} else {
			return esc_html__( 'Please add your Cookiebot ID to show Cookie Declarations', 'cookiebot' );
		}
	}

Code file location:

cookiebot/cookiebot/src/shortcode/Cookiebot_Declaration_Shortcode.php

Conclusion

Now that you’ve learned how to embed the Cookiebot Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *