Cookie Notice & Compliance for GDPR / CCPA Shortcodes

Below, you’ll find a detailed guide on how to add the Cookie Notice & Compliance for GDPR / CCPA Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Cookie Notice & Compliance for GDPR / CCPA Plugin shortcodes not to show or not to work correctly.

Before starting, here is an overview of the Cookie Notice & Compliance for GDPR / CCPA Plugin and the shortcodes it provides:

Plugin Icon
Cookie Notice & Compliance for GDPR / CCPA

"Cookie Notice & Compliance for GDPR/CCPA is a WordPress plugin that ensures your website adheres to the latest GDPR and CCPA regulations. It handles user consent for cookies in a transparent and efficient manner."

★★★★☆ (2961) Active Installs: 1000000+ Tested with: 6.3.2 PHP Version: 5.4
Included Shortcodes:
  • [cookies_accepted]
  • [cookies_revoke]
  • [cookies_policy_link]

Cookie Notice & Compliance for GDPR / CCPA [cookies_accepted] Shortcode

The Cookie Notice shortcode enables conditional content display based on user’s cookie acceptance. It checks if cookies are accepted, decodes HTML entities, and filters content. If scripts are present and shortcodes are detected, they’re executed and returned. If no cookies are accepted, it returns an empty string.

Shortcode: [cookies_accepted]

Examples and Usage

Basic example – A straightforward usage of the ‘cookies_accepted’ shortcode. This example shows how to use the shortcode without any parameters. It will execute the associated function and return the content if cookies have been accepted by the user.

[cookies_accepted]

PHP Function Code

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

Shortcode line:

add_shortcode( 'cookies_accepted', [ $this, 'cookies_accepted_shortcode' ] );

Shortcode PHP function:

function cookies_accepted_shortcode( $args, $content ) {
		if ( $this->cookies_accepted() ) {
			$scripts = html_entity_decode( trim( wp_kses( $content, $this->get_allowed_html() ) ) );

			if ( ! empty( $scripts ) ) {
				if ( preg_match_all( '/' . get_shortcode_regex() . '/', $content ) )
					$scripts = do_shortcode( $scripts );

				return $scripts;
			}
		}

		return '';
	}

Code file location:

cookie-notice/cookie-notice/cookie-notice.php

Cookie Notice & Compliance for GDPR / CCPA [cookies_revoke] Shortcode

The Cookie Notice shortcode is a powerful tool that allows users to revoke their cookie consent. It generates a clickable link, which when clicked, retracts the user’s consent. The shortcode fetches options from the general settings. It’s compatible with WPML and Polylang, translating the revoke text accordingly. The shortcode generates a link with the class and title specified in the settings. If the cookie notice status is active, a data attribute is added to the link for revoking consent.

Shortcode: [cookies_revoke]

Parameters

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

  • title – Defines the text displayed on the revoke button
  • class – Specifies a custom CSS class for the revoke button

Examples and Usage

Basic example – Show a revoke cookie notice with default settings.

[cookies_revoke /]

Advanced examples

Display a revoke cookie notice with a custom title and CSS class.

[cookies_revoke title="Cancel Cookies" class="my-custom-class" /]

Display a revoke cookie notice with a different title. The title will be displayed on the button.

[cookies_revoke title="Revoke Cookies" /]

Use a custom CSS class for the revoke cookie notice. This allows you to style the button with your own CSS.

[cookies_revoke class="my-custom-class" /]

PHP Function Code

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

Shortcode line:

add_shortcode( 'cookies_revoke', [ $this, 'cookies_revoke_shortcode' ] );

Shortcode PHP function:

function cookies_revoke_shortcode( $args, $content ) {
		// get options
		$options = $this->options['general'];

		// WPML >= 3.2
		if ( defined( 'ICL_SITEPRESS_VERSION' ) && version_compare( ICL_SITEPRESS_VERSION, '3.2', '>=' ) )
			$options['revoke_text'] = apply_filters( 'wpml_translate_single_string', $options['revoke_text'], 'Cookie Notice', 'Revoke button text' );
		// WPML and Polylang compatibility
		elseif ( function_exists( 'icl_t' ) )
			$options['revoke_text'] = icl_t( 'Cookie Notice', 'Revoke button text', $options['revoke_text'] );

		// defaults
		$defaults = [
			'title'	=> $options['revoke_text'],
			'class'	=> $options['css_class']
		];

		// combine shortcode arguments
		$args = shortcode_atts( $defaults, $args );

		if ( Cookie_Notice()->get_status() === 'active' )
			$shortcode = '<a href="#" class="cn-revoke-cookie cn-button-inline cn-revoke-inline' . esc_attr( $args['class'] !== '' ? ' ' . $args['class'] : '' ) . '" title="' . esc_attr( $args['title'] ) . '" data-hu-action="cookies-notice-revoke">' . esc_html( $args['title'] ) . '</a>';
		else
			$shortcode = '<a href="#" class="cn-revoke-cookie cn-button-inline cn-revoke-inline' . esc_attr( $args['class'] !== '' ? ' ' . $args['class'] : '' ) . '" title="' . esc_attr( $args['title'] ) . '">' . esc_html( $args['title'] ) . '</a>';

		return $shortcode;
	}

Code file location:

cookie-notice/cookie-notice/cookie-notice.php

Cookie Notice & Compliance for GDPR / CCPA [cookies_policy_link] Shortcode

The Cookie Notice shortcode is a tool that generates a link to your privacy policy. This shortcode retrieves the link to your privacy policy page or a custom URL based on your settings. The shortcode supports WPML and Polylang for multilingual sites and is compatible with multisite installations. It also allows customization of the link’s CSS class and title.

Shortcode: [cookies_policy_link]

Parameters

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

  • title – The text displayed for the cookie notice link.
  • link – The URL where the user will be redirected.
  • class – CSS class applied to the cookie notice link.

Examples and Usage

Basic example – Display a link to the privacy policy page with the default text and CSS class as defined in the plugin settings.

[cookies_policy_link /]

Advanced examples

Display a link to the privacy policy page with custom text and a custom CSS class. The privacy policy page is defined in the plugin settings.

[cookies_policy_link title="Check our Privacy Policy" class="my-custom-class" /]

Display a link to a custom URL with custom text and a custom CSS class. The URL is not defined in the plugin settings but directly in the shortcode.

[cookies_policy_link title="Check our Privacy Policy" link="https://www.example.com/privacy-policy/" class="my-custom-class" /]

Display a link to the privacy policy page with custom text. The CSS class is defined in the plugin settings.

[cookies_policy_link title="Check our Privacy Policy" /]

Display a link to the privacy policy page with a custom CSS class. The text is defined in the plugin settings.

[cookies_policy_link class="my-custom-class" /]

PHP Function Code

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

Shortcode line:

add_shortcode( 'cookies_policy_link', [ $this, 'cookies_policy_link_shortcode' ] );

Shortcode PHP function:

function cookies_policy_link_shortcode( $args, $content ) {
		// get options
		$options = $this->options['general'];

		// WPML >= 3.2
		if ( defined( 'ICL_SITEPRESS_VERSION' ) && version_compare( ICL_SITEPRESS_VERSION, '3.2', '>=' ) ) {
			$options['see_more_opt']['text'] = apply_filters( 'wpml_translate_single_string', $options['see_more_opt']['text'], 'Cookie Notice', 'Privacy policy text' );
			$options['see_more_opt']['link'] = apply_filters( 'wpml_translate_single_string', $options['see_more_opt']['link'], 'Cookie Notice', 'Custom link' );
		// WPML and Polylang compatibility
		} elseif ( function_exists( 'icl_t' ) ) {
			$options['see_more_opt']['text'] = icl_t( 'Cookie Notice', 'Privacy policy text', $options['see_more_opt']['text'] );
			$options['see_more_opt']['link'] = icl_t( 'Cookie Notice', 'Custom link', $options['see_more_opt']['link'] );
		}

		if ( $options['see_more_opt']['link_type'] === 'page' ) {
			// multisite with global override?
			if ( is_multisite() && $this->is_plugin_network_active() && $this->network_options['global_override'] ) {
				// get main site id
				$main_site_id = get_main_site_id();

				// switch to main site
				switch_to_blog( $main_site_id );

				// update page id for current language if needed
				if ( function_exists( 'icl_object_id' ) )
					$options['see_more_opt']['id'] = icl_object_id( $options['see_more_opt']['id'], 'page', true );

				// get main site privacy policy link
				$permalink = get_permalink( $options['see_more_opt']['id'] );

				// restore current site
				restore_current_blog();
			} else {
				// update page id for current language if needed
				if ( function_exists( 'icl_object_id' ) )
					$options['see_more_opt']['id'] = icl_object_id( $options['see_more_opt']['id'], 'page', true );

				// get privacy policy link
				$permalink = get_permalink( $options['see_more_opt']['id'] );
			}
		}

		// defaults
		$defaults = [
			'title'	=> $options['see_more_opt']['text'] !== '' ? $options['see_more_opt']['text'] : '&#x279c;',
			'link'	=> $options['see_more_opt']['link_type'] === 'custom' ? $options['see_more_opt']['link'] : $permalink,
			'class'	=> $options['css_class']
		];

		// combine shortcode arguments
		$args = shortcode_atts( $defaults, $args );

		$shortcode = '<a href="' . esc_url( $args['link'] ) . '" target="' . esc_attr( $options['link_target'] ) . '" id="cn-more-info" class="cn-privacy-policy-link cn-link' . esc_attr( $args['class'] !== '' ? ' ' . $args['class'] : '' ) . '">' . esc_html( $args['title'] ) . '</a>';

		return $shortcode;
	}

Code file location:

cookie-notice/cookie-notice/cookie-notice.php

Conclusion

Now that you’ve learned how to embed the Cookie Notice & Compliance for GDPR / CCPA Plugin shortcodes, 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 *