Complianz Cookie banner FIX
Do you use Complianz? Cookie banner is not showing in /portal/ slug inside FC?
Feel free to use my snippet below. It should help.
<?php
/**
- DriveZone β Complianz Pro uvnitΕ FluentCommunity portΓ‘lu
- ProblΓ©m: FluentCommunity renderuje portΓ‘l vlastnΓ Ε‘ablonou (SPA) a nevolΓ‘
- wp_head() / wp_footer(), takΕΎe Complianz nevloΕΎΓ CSS, JS ani HTML banneru.
- ΕeΕ‘enΓ: na hooky portΓ‘lu (fluent_community/portal_head + portal_footer)
- spustΓme POUZE ty callbacky z wp_enqueue_scripts / wp_head / wp_footer,
- kterΓ© pochΓ‘zΓ ze souborΕ― Complianzu (+ WP Consent API), a vytiskneme jen
- jejich zaΕazenΓ© styly a skripty. OstatnΓ pluginy/Ε‘ablona se do portΓ‘lu
- nedostanou, takΕΎe SPA zΕ―stane ΔistΓ© a rychlΓ©.
- Code Snippets: typ PHP, "Run everywhere" (frontend).
- Debug: jako admin otevΕi portΓ‘l s ?cmplz_fc_debug=1 a v HTML najdi komentΓ‘Ε
- "DZ Complianz FC".
*/
if (!defined('ABSPATH')) {
return;
}
if (!function_exists('dz_cmplz_fc_matches')) {
/** ΕetΔzce, podle kterΓ½ch poznΓ‘me Complianz soubory / handly. */
function dz_cmplz_fc_needles()
{
return (array) apply_filters('dz_cmplz_fc_needles', ['complianz', 'cmplz', 'wp-consent-api']);
}
function dz_cmplz_fc_matches($str)
{
$str = strtolower(wp_normalize_path((string) $str));
foreach (dz_cmplz_fc_needles() as $needle) {
if ($needle !== '' && strpos($str, $needle) !== false) {
return true;
}
}
return false;
}
/** VrΓ‘tΓ cestu k souboru, kde je callback definovΓ‘n (nebo ''). */
function dz_cmplz_fc_callback_file($fn)
{
try {
if ($fn instanceof Closure) {
$r = new ReflectionFunction($fn);
} elseif (is_string($fn) && strpos($fn, '::') !== false) {
list($class, $method) = explode('::', $fn, 2);
$r = new ReflectionMethod($class, $method);
} elseif (is_string($fn)) {
if (!function_exists($fn)) {
return '';
}
$r = new ReflectionFunction($fn);
} elseif (is_array($fn) && count($fn) === 2) {
$r = new ReflectionMethod($fn[0], $fn[1]);
} elseif (is_object($fn) && method_exists($fn, '__invoke')) {
$r = new ReflectionMethod($fn, '__invoke');
} else {
return '';
}
return (string) $r->getFileName();
} catch (\Throwable $e) {
return '';
}
}
/** SpustΓ jen Complianz callbacky danΓ©ho hooku (kaΕΎdΓ½ hook max. jednou). */
function dz_cmplz_fc_run_hook($hook)
{
static $done = [];
global $wp_filter;
if (isset($done[$hook]) || empty($wp_filter[$hook])) {
return [];
}
$done[$hook] = true;
$log = [];
$callbacks = $wp_filter[$hook]->callbacks; // WP_Hook, uΕΎ seΕazeno podle priority
ksort($callbacks, SORT_NUMERIC);
foreach ($callbacks as $priority => $items) {
foreach ($items as $id => $cb) {
$file = dz_cmplz_fc_callback_file($cb['function']);
if ($file === '' || !dz_cmplz_fc_matches($file)) {
continue;
}
try {
// do_action('wp_head') pΕedΓ‘vΓ‘ jeden prΓ‘zdnΓ½ argument
$args = array_slice([''], 0, max(0, (int) $cb['accepted_args']));
call_user_func_array($cb['function'], $args);
$log[] = "{$hook} @{$priority}: {$id}";
} catch (\Throwable $e) {
$log[] = "CHYBA {$hook} @{$priority}: {$id} β " . $e->getMessage();
}
}
}
return $log;
}
/** Handly zaΕazenΓ½ch assetΕ―, kterΓ© patΕΓ Complianzu. */
function dz_cmplz_fc_handles($deps)
{
$out = [];
foreach ((array) $deps->queue as $handle) {
$src = isset($deps->registered[$handle]) ? (string) $deps->registered[$handle]->src : '';
if (dz_cmplz_fc_matches($handle . ' ' . $src)) {
$out[] = $handle;
}
}
return $out;
}
function dz_cmplz_fc_log($lines)
{
$GLOBALS['dz_cmplz_fc_log'] = array_merge($GLOBALS['dz_cmplz_fc_log'] ?? [], (array) $lines);
}
/* ---------------------------------------------------------------------
* <head> portΓ‘lu
* ------------------------------------------------------------------ */
add_action('fluent_community/portal_head', function () {
// Pokud portΓ‘l bΔΕΎΓ pΕes hlaviΔku Ε‘ablony, Complianz funguje nativnΔ.
if (did_action('wp_head')) {
return;
}
dz_cmplz_fc_log(dz_cmplz_fc_run_hook('wp_enqueue_scripts'));
dz_cmplz_fc_log(dz_cmplz_fc_run_hook('wp_head'));
$styles = dz_cmplz_fc_handles(wp_styles());
if ($styles) {
wp_print_styles($styles);
}
dz_cmplz_fc_log('styles: ' . implode(', ', $styles));
?>
<style id="dz-cmplz-fc">
/* banner nad hlaviΔkou / modΓ‘ly FluentCommunity */
#cmplz-cookiebanner-container,
#cmplz-manage-consent {
display: block !important;
visibility: visible !important;
opacity: 1 !important;
transform: none !important;
filter: none !important;
contain: none !important;
}
.cmplz-cookiebanner.cmplz-show {
display: grid !important;
visibility: visible !important;
opacity: 1 !important;
position: fixed !important;
z-index: 2147483000 !important;
pointer-events: auto !important;
/* ukotvenΓ do pravΓ©ho dolnΓho rohu viewportu */
top: auto !important;
left: auto !important;
right: 10px !important;
bottom: 10px !important;
transform: none !important;
margin: 0 !important;
max-width: calc(100vw - 20px) !important;
max-height: calc(100dvh - 20px) !important;
overflow-y: auto !important;
}
<a data-user_name="media" class="fcom_mention fcom_route" href="https://community.wpmanageninja.com/portal/u/media/">Media Rocketeer</a> (max-width: 768px) {
.cmplz-cookiebanner.cmplz-show {
right: 10px !important;
left: 10px !important;
width: auto !important;
}
}
#cmplz-manage-consent .cmplz-manage-consent { z-index: 2147482999 !important; }
</style>
<?php
}, 20);
/* ---------------------------------------------------------------------
* PatiΔka portΓ‘lu β HTML banneru + skripty
* ------------------------------------------------------------------ */
add_action('fluent_community/portal_footer', function () {
if (did_action('wp_footer')) {
return;
}
// Pojistka, kdyby portal_head neprobΔhl
dz_cmplz_fc_log(dz_cmplz_fc_run_hook('wp_enqueue_scripts'));
// HTML banneru a pΕΓpadnΓ© footer vΓ½stupy Complianzu
dz_cmplz_fc_log(dz_cmplz_fc_run_hook('wp_footer'));
$styles = dz_cmplz_fc_handles(wp_styles());
if ($styles) {
wp_print_styles($styles); // uΕΎ vytiΕ‘tΔnΓ© se znovu netisknou
}
$scripts = dz_cmplz_fc_handles(wp_scripts());
if ($scripts) {
wp_print_scripts($scripts); // vΔetnΔ lokalizace (objekt complianz)
}
dz_cmplz_fc_log('scripts: ' . implode(', ', $scripts));
?>
<script id="dz-cmplz-fc-js">
(function () {
// PΕesune kontejnery Complianzu na konec <body>, pryΔ z wrapperΕ― portΓ‘lu
// (overflow:hidden / transform / contain u pΕedka rozbΓjΓ position:fixed).
// MutationObserver hlΓdΓ‘, kdyby je SPA znovu pΕesunula nebo zahodila.
var ids = ['cmplz-cookiebanner-container', 'cmplz-manage-consent'];
function lift() {
ids.forEach(function (id) {
var el = document.getElementById(id);
if (el && el.parentNode !== document.body) {
document.body.appendChild(el);
}
});
}
lift();
document.addEventListener('DOMContentLoaded', lift);
window.addEventListener('load', lift);
try {
new MutationObserver(lift).observe(document.body, { childList: true });
} catch (e) {}
// Odkaz na znovuotevΕenΓ banneru funguje i v SPA:
// - libovolnΓ½ prvek s tΕΓdou .cmplz-show-banner
// - odkaz s URL konΔΓcΓ na #cookie-nastaveni (napΕ. vlastnΓ poloΕΎka menu ve FC)
document.addEventListener('click', function (e) {
var t = e.target.closest && e.target.closest('.cmplz-show-banner, a[href$="#cookie-nastaveni"]');
if (!t) return;
e.preventDefault();
e.stopPropagation(); // aΕ₯ to nechytΓ router FluentCommunity
var btn = document.querySelector('.cmplz-manage-consent');
if (btn) btn.click();
}, true);
})();
</script>
<?php
if (
isset($_GET['cmplz_fc_debug']) &&
is_user_logged_in() &&
current_user_can('manage_options')
) {
echo "\n<!-- DZ Complianz FC\n" . esc_html(implode("\n", $GLOBALS['dz_cmplz_fc_log'] ?? [])) . "\n-->\n";
}
}, 20);
}
Hi thanks :)
Hey Drive ZoneΒ , does that mean you found a solution to add it to FC?
AndrΓ© DausΒ Yes, use my snippet and it should work. For me it works now.
Drive ZoneΒ great. Then, I do not need to look up my solution. I used a different option to add it to the header. But since I do not use Complianz, I do not have it at hand.
Great you solved it.