Cookie Code breaks site – Set your own Cookie based on a (custom) Consent Categ
-
Hello
I am following your guide here https://complianz.io/set-your-own-cookie-based-on-a-custom-consent-category/I created a .php called cloudflare-consent.php and uploaded it to the MU Plugins folder
but when I add the second part “Adding a custom cookie”
that code breaks the sitefunction cmplz_set_rlx() { ob_start(); ?> <script> function cmplz_set_rlx_cookie(value){ let secure = ";secure"; let date = new Date(); let name = 'rlx-consent'; date.setTime(date.getTime() + (complianz.cookie_expiry * 24 * 60 * 60 * 1000)); let expires = ";expires=" + date.toGMTString(); console.log("set cookie with string "+name + "=" + value + ";SameSite=Lax" + secure + expires); document.cookie = name + "=" + value + ";SameSite=Lax" + secure + expires; } /** * Set a complianz rlx cookie and the custom cookie on save preferences */ document.addEventListener('click', e => { if (e.target.closest('.cmplz-save-preferences')) { let rlx_enabled = document.querySelector('input.cmplz-functional').checked; if (rlx_enabled) { console.log("save prefs, rlx enabled"); cmplz_set_cookie('rlx', 'allow'); cmplz_set_rlx_cookie(true); } else { console.log("save prefs, rlx disabled"); cmplz_set_rlx_cookie(false); cmplz_set_cookie('rlx', 'deny'); } } }); document.addEventListener("cmplz_fire_categories", function (e) { var consentedCategories = e.detail.categories; if ( cmplz_in_array( 'marketing', consentedCategories ) ) { cmplz_set_cookie('rlx', 'allow'); } if ( cmplz_in_array( 'rlx', consentedCategories ) ) { cmplz_set_cookie('rlx', 'allow'); } }); document.addEventListener("cmplz_before_cookiebanner", function(e) { if ( cmplz_get_cookie('rlx') === 'allow'){ console.log("set checkbox rlx to true"); document.querySelector('input.cmplz-functional').checked = true; _satellite.setVar("Analyticsconsent","true"); _satellite.track("PageView"); } else { _satellite.setVar("Analyticsconsent","false"); //set a default false value, as workaround for rlx issue which checks for false cookie, instead of also checking for not-existing one. cmplz_set_rlx_cookie(false); } }); /** * Handle revoke */ document.addEventListener("cmplz_revoke", function(e) { console.log("revoke rlx cookie"); cmplz_set_cookie('rlx', 'deny'); cmplz_set_rlx_cookie(false); }); </script> <?php $script = ob_get_clean(); $script = str_replace(array('<script>', '</script>'), '', $script); wp_add_inline_script( 'cmplz-cookiebanner', $script ); } add_action( 'wp_enqueue_scripts', 'cmplz_set_rlx',PHP_INT_MAX );
I added the code in a code snippets plugin first and it would not activate because of an error, so then I thought I must add it to the bottom of the mu plugin I created, but that breaks the site.
Please assist thanks
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘Cookie Code breaks site – Set your own Cookie based on a (custom) Consent Categ’ is closed to new replies.