Beautiful Cookie Consent Banner Shortcodes

Below, you’ll find a detailed guide on how to add the Beautiful Cookie Consent Banner 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 Beautiful Cookie Consent Banner Plugin shortcodes not to show or not to work correctly.

Before starting, here is an overview of the Beautiful Cookie Consent Banner Plugin and the shortcodes it provides:

Plugin Icon
Beautiful Cookie Consent Banner

"Beautiful Cookie Consent Banner is an easy-to-use WordPress plugin designed to create an attractive and responsive cookie consent notification banner, ensuring your website adheres to GDPR rules."

★★★★✩ (47) Active Installs: 40000+ Tested with: 6.3.2 PHP Version: 5.2.17
Included Shortcodes:
  • [cc_revoke_settings_link_nsc_bar]
  • [cc_show_cookie_banner_nsc_bar]

Beautiful Cookie Consent Banner [cc_revoke_settings_link_nsc_bar] Shortcode

The ‘cc_revoke_settings_link_nsc_bar’ shortcode is designed to manage cookie consent on your WordPress site. It allows users to opt-in or opt-out of cookies, ensuring GDPR compliance. This shortcode retrieves cookie settings like name, domain, and expiry days. It then determines the compliance type (opt-in or opt-out) and sets the appropriate cookie value. It also handles the link text for both opted-in and opted-out states. The shortcode is smart enough to handle localhost and live environments differently. Finally, it creates an anchor tag with necessary data attributes and an onclick event to manage cookie consent, providing a seamless user experience.

Shortcode: [cc_revoke_settings_link_nsc_bar]

Parameters

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

  • data-link_text_after_click – The text displayed on the link after it is clicked.
  • data-link_text_before_click – The text displayed on the link before it is clicked.
  • data-cookiename – The name of the cookie used for tracking consent.
  • data-current_cookie_value – The current value of the cookie used for tracking consent.
  • data-cookie_value_after_click – The value of the cookie after the link is clicked.
  • data-expires – The expiration time of the cookie in seconds.
  • data-domain – The domain where the cookie is applicable.
  • id – The unique identifier of the link for CSS styling and JavaScript targeting.

Examples and Usage

Basic example – A simple usage of the shortcode to revoke the cookie settings. The shortcode does not require any parameters or attributes to function correctly.

[cc_revoke_settings_link_nsc_bar /]

Advanced examples

Using the shortcode to revoke the cookie settings with custom text for opted in and opted out states. The shortcode uses two attributes ‘link_text_opted_in’ and ‘link_text_opted_out’ to customize the text displayed to the user.

[cc_revoke_settings_link_nsc_bar link_text_opted_in="You are opted in" link_text_opted_out="You are opted out" /]

Using the shortcode to revoke the cookie settings with custom cookie name and domain. The shortcode uses two attributes ‘cookie_name’ and ‘cookie_domain’ to specify the cookie name and domain respectively.

[cc_revoke_settings_link_nsc_bar cookie_name="my_custom_cookie" cookie_domain="my_custom_domain.com" /]

Using the shortcode to revoke the cookie settings with custom cookie expiry days. The shortcode uses the ‘cookie_expiryDays’ attribute to specify the number of days before the cookie expires.

[cc_revoke_settings_link_nsc_bar cookie_expiryDays="30" /]

PHP Function Code

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

Shortcode line:

add_shortcode('cc_revoke_settings_link_nsc_bar', array($this, 'nsc_bar_shortcode_revoke_settings_link'));

Shortcode PHP function:

function nsc_bar_shortcode_revoke_settings_link()
    {
        $banner_configs_obj = new nsc_bar_banner_configs();

        $cookie_name = $banner_configs_obj->nsc_bar_get_cookie_setting("cookie_name", $this->plugin_configs->nsc_bar_return_settings_field_default_value("cookie_name"));
        $cookie_domain = $banner_configs_obj->nsc_bar_get_cookie_setting("cookie_domain", $this->plugin_configs->nsc_bar_return_settings_field_default_value("cookie_domain"));
        $cookie_expiry_days = $banner_configs_obj->nsc_bar_get_cookie_setting("cookie_expiryDays", $this->plugin_configs->nsc_bar_return_settings_field_default_value("cookie_expiryDays"));
        $compliance_type = $banner_configs_obj->nsc_bar_get_cookie_setting("type", $this->plugin_configs->nsc_bar_return_settings_field_default_value("type"));

        $link_text_opted_in = $this->plugin_configs->nsc_bar_get_option("shortcode_link_text_opted_in");
        $link_text_opted_out = $this->plugin_configs->nsc_bar_get_option("shortcode_link_text_opted_out");

        $wordpressUrl = get_bloginfo('url');
        $hostname = parse_url($wordpressUrl, PHP_URL_HOST);
        if ($hostname == "localhost") {
            $domain[0] = "localhost";
        } else {
            $hostname = $cookie_domain;
        }

        switch ($compliance_type) {
            case "opt-out":
                $current_cookie_value = "allow";
                break;
            case "opt-in":
                $current_cookie_value = "deny";
                break;
            default:
                return null;
        }

        if (isset($_COOKIE[$cookie_name]) && $current_cookie_value != "dismiss") {
            $current_cookie_value = $_COOKIE[$cookie_name];
        }

        switch ($current_cookie_value) {
            case "allow":
                $linktext = $link_text_opted_in;
                $linktext_after_click = $link_text_opted_out;
                $cookie_value_after_click = "deny";
                break;
            case "deny":
                $linktext = $link_text_opted_out;
                $linktext_after_click = $link_text_opted_in;
                $cookie_value_after_click = "allow";
                break;
            default:
                $linktext = "";
                $linktext_after_click = "";
                $cookie_value_after_click = "";
        }

        if (isset($_COOKIE[$cookie_name]) && $current_cookie_value != $_COOKIE[$cookie_name]) {
            $linktext = $atts['link_text_opted_out'];
        }

        $expire = time() + 60 * 60 * 24 * $cookie_expiry_days;
        $js_code = preg_replace("/\r|\n/", "", file_get_contents(NSC_BAR_PLUGIN_DIR . "/public/revoke_shortcode.js"));
        return "<a id='nsc_bar_optout_link' data-link_text_after_click='" . esc_attr($linktext_after_click) . "' data-link_text_before_click='" . esc_attr($linktext) . "' data-cookiename='" . esc_attr($cookie_name) . "' data-current_cookie_value='" . esc_attr($current_cookie_value) . "'data-cookie_value_after_click='" . esc_attr($cookie_value_after_click) . "' data-expires='" . esc_attr($expire) . "' data-domain='" . esc_attr($hostname) . "' style='cursor: pointer;' onclick='" . $js_code . "'>" . esc_html($linktext) . "</a>";
    }

Code file location:

beautiful-and-responsive-cookie-consent/beautiful-and-responsive-cookie-consent/class/class-nsc_bar_frontend.php

Beautiful Cookie Consent Banner [cc_show_cookie_banner_nsc_bar] Shortcode

The ‘cc_show_cookie_banner_nsc_bar’ shortcode from the Beautiful and Responsive Cookie Consent plugin is used to display a clickable cookie consent banner. This shortcode fetches the banner text from the plugin’s settings and wraps it in a link. The ‘id’ attribute of the link is set to ‘nsc_bar_link_show_banner’, and its style is set to show a pointer cursor when hovered.

Shortcode: [cc_show_cookie_banner_nsc_bar]

Examples and Usage

Basic example – A simple usage of the shortcode to display a cookie consent banner with default link text. This shortcode will generate a clickable link that, when clicked, will display the cookie consent banner.

[cc_show_cookie_banner_nsc_bar /]

Advanced examples

Utilizing the shortcode to display a cookie consent banner with custom link text. In this example, the link text is set to “Click here to view cookies”. When this link is clicked, the cookie consent banner will be displayed.


add_filter('nsc_bar_get_option', function($value, $option_name){
    if($option_name == 'shortcode_link_show_banner_text') {
        return 'Click here to view cookies';
    }
    return $value;
}, 10, 2);

Note: The above example uses the ‘nsc_bar_get_option’ filter to change the link text. The filter function checks if the current option is ‘shortcode_link_show_banner_text’ and if so, changes the value to ‘Click here to view cookies’. If the current option is not ‘shortcode_link_show_banner_text’, the original value is returned. This ensures that only the link text of the cookie consent banner is changed, and not any other options.

PHP Function Code

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

Shortcode line:

add_shortcode('cc_show_cookie_banner_nsc_bar', array($this, 'nsc_bar_shortcode_show_cookie_banner'));

Shortcode PHP function:

function nsc_bar_shortcode_show_cookie_banner()
    {
        $linktext = $this->plugin_configs->nsc_bar_get_option("shortcode_link_show_banner_text");
        return "<a id='nsc_bar_link_show_banner' style='cursor: pointer;'>" . esc_html($linktext) . "</a>";
    }

Code file location:

beautiful-and-responsive-cookie-consent/beautiful-and-responsive-cookie-consent/class/class-nsc_bar_frontend.php

Conclusion

Now that you’ve learned how to embed the Beautiful Cookie Consent Banner 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 *