Skip to main content

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);

}

Nicolas D

Hi thanks :)

AndrΓ© Daus

Hey Drive ZoneΒ , does that mean you found a solution to add it to FC?

Drive Zone

AndrΓ© DausΒ Yes, use my snippet and it should work. For me it works now.

AndrΓ© Daus

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.