Link to home
Start Free TrialLog in
Avatar of Jon Imms
Jon ImmsFlag for United States of America

asked on

Remove Cookie Consent banner markup after consent accepted

Hello, We are currently using a Cookie Consent plugin on our wordpress website - https://www.webtoffee.com/product/gdpr-cookie-consent/


We are building a new site, and would like to get some performance improvements out of it,   so for this plugin,  when you accept the cookie,  the banner is just hidden, but the rendered HTML is still in the DOM even though it's been accepted. 


We would like to, when the cookie is accepted, declined  remove the unneeded html markup from the DOM. 


I was given this to work off -


add_filter('cli_show_cookie_bar_only_on_selected_pages', 'bt_testing_cookieconsent_filter', 10, 1);

function bt_testing_cookieconsent_filter($cookie_consent_render){
   write_log(explode('; ', $_SERVER['HTTP_COOKIE']));
   return $cookie_consent_render;
}

Open in new window

I would create this as a simple extension plugin to the webtoffee plugin we are currently using, this way we can easily migrate this functionality with the other plugin regardless of theme. 

Just check for cookies (like cookielawinfo-checkbox-functional) in the request using the _SERVER super global and conditionally pass an empty string to prevent the HTML from being rendered.
 


I've created the plugin,  but not really sure how to do the rest of what is asked?


<?php
/**
 * Plugin Name:       BT Cookie Consent
 * Description:       Extends the WebToffee plugin's functionality for our Cookie Consent
 * Requires at least: 5.8
 * Requires PHP:      7.4
 * Version:           0.1.1
 * Author:            JI
 * License:           GPL-2.0-or-later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       bt-cookie-consent
 */

add_filter('cli_show_cookie_bar_only_on_selected_pages', 'bt_testing_cookieconsent_filter', 10, 1);

function bt_testing_cookieconsent_filter($cookie_consent_render){
    write_log(explode('; ', $_SERVER['HTTP_COOKIE']));
    return $cookie_consent_render;
}

Open in new window

Could somebody help me with this please?

Link to the website = Website here

Avatar of Scott Fell
Scott Fell
Flag of United States of America image

I think you just need to create a folder in your plugins folder under your wp-content folder. 


You can call it BT-Cookie-Consent, then name the file BT-Cookie-Consent.php

ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

Hi Jon,


If you're just looking for guidance on how to actually create/install a plugin, it's really, really easy, but I'll walk you through it.


So first off, ALL plugins go into the wp-content/plugins folder of your WordPress (WP) folder.


Plugins can either be a single file (for pretty simple plugins) or they can be in a sub-folder (for more complex plugins that have more than one file). In your case, your plugin is really simple, so you can just create a new yourplugin.php file in your plugins folder, like this:


.../wp-content/plugins/btcookieconsent.php


When you're creating single-file plugins like this, the filename doesn't really matter. You could call it foobar.php if you wanted. What really matters is that comment block at the top. The WP admin area will look for that comment block and parse it for information about the plugin. For example, it literally looks for a line that contains "Plugin Name:" in that comment block and then says, "Whatever is after that is the name of the plugin" and so on.


Once the plugin file(s) are in place, you then go to your admin area, go to the plugins section. That plugins page will scan the wp-content/plugins folder, read all the files, and present that list, and you'll see the new one in there, but it won't be activated yet:


User generated image

WP manages plugin activation via a record in the database. So when you click on Activate, it'll write a record into the database indicating that you've activated that plugin.


When the regular WP content page loads, WP will query the DB for all activated plugins, then load them all up and from there, the code inside the plugin is what matters. So in this case, your main code is this line here:


add_filter('cli_show_cookie_bar_only_on_selected_pages', 'bt_testing_cookieconsent_filter', 10, 1);


That says, "When something in the current page runs a filter called "cli_show_cookie_bar_only_on_selected_pages", then run the function "bt_testing_cookieconsent_filter" at priority 10 (priority indicates the order in which plugins run in case there are more than one that are listening for the same filter - the lower the number, the earlier it runs).


A filter is basically there to allow you to modify something.


Original Content -> Filter Plugin -> Modified Content returned from Filter


So if you wanted a filter that simply changed everything to uppercase, you'd have a filter function like this:


function an_uppercasing_filter_function($data)
{
  return strtoupper($data);
}

Open in new window


The result would be:

Content xYz -> an_uppercasing_filter_function -> CONTENT XYZ


So in your particular case, the presumption is that this WebToffee plugin is creating some HTML for the banner and then passing the HTML to a filter hook called cli_show_cookie_bar_only_on_selected_pages. 


Your plugin would then return an empty string "" if you wanted to suppress that content.


So Chris's function will probably do the right thing, but hopefully you now understand how to install the plugin and how/why it works.



Also, Scott's instructions on how to install it should also work just fine. While you don't absolutely need a sub-folder for single-file plugins, some people just do that just in case they want to expand the plugin later, and it should work either way. I was simply trying to add a little more context.

Avatar of Jon Imms

ASKER

Both really helpful, thank you!!    I've created the plugin, activated, and know what the function is doing/looking for now.   Not working with that particular cookie atm, but i think if i fiddle around with that function, i should beable to get this to work.   it's the <div class="wt-cli-cookie-bar-container" data-nosnippet="true">  div which we are trying to target/remove 

For something small like this, you should be able to just add that function to your functions.php instead of creating a plugin. I don't know if it is best practice, but because it is just the one small function, I tend to put it in my functions.php and save plug ins for something that needs more robust code.

this is how i got it working 


add_filter('cli_show_cookie_bar_only_on_selected_pages', 'bt_testing_cookieconsent_filter', 10, 1);
/*
 * Check Cookie is set, and not empty.
 */
function bt_testing_cookieconsent_filter($html){
    if (isset($_COOKIE) && !empty($_COOKIE) && isset($_COOKIE['CookieLawInfoConsent']) && !empty($_COOKIE['CookieLawInfoConsent'])) {
        return "";
    }
    if (isset($_SERVER['HTTP_COOKIE']['CookieLawInfoConsent']) && !empty($_SERVER['HTTP_COOKIE']['CookieLawInfoConsent'])) {
        return "";
    }
    return $html;
}

Open in new window