Halloween Sale 15% off Ends in See the plans
PHP

Hold Tracking Scripts Until the Visitor Consents

Turns the scripts you list by handle into inactive placeholders and only runs them after the visitor gives consent, through the WP Consent API when a consent plugin provides it or the simple wpide_consent cookie otherwise.

Don’t have WPIDE yet? Install it on your site

Code

// Script handle => consent category (functional, preferences, statistics, marketing).
$wpide_consent_scripts = array(
    'google-analytics' => 'statistics',
    'facebook-pixel'   => 'marketing',
);

add_filter( 'script_loader_tag', function ( $tag, $handle ) use ( $wpide_consent_scripts ) {
    if ( empty( $wpide_consent_scripts[ $handle ] ) || ! class_exists( 'WP_HTML_Tag_Processor' ) ) {
        return $tag;
    }

    $tags = new WP_HTML_Tag_Processor( $tag );

    while ( $tags->next_tag( 'script' ) ) {
        $tags->set_attribute( 'type', 'text/plain' );
        $tags->set_attribute( 'data-wpide-consent', $wpide_consent_scripts[ $handle ] );
    }

    return $tags->get_updated_html();
}, 20, 2 );

add_action( 'wp_footer', function () {
    ?>
<script>
(function () {
    function allowed(category) {
        if (typeof window.wp_has_consent === 'function') { return window.wp_has_consent(category); }
        return /(?:^|; )wpide_consent=granted/.test(document.cookie);
    }
    function release() {
        document.querySelectorAll('script[type="text/plain"][data-wpide-consent]').forEach(function (old) {
            if (!allowed(old.getAttribute('data-wpide-consent'))) { return; }
            var s = document.createElement('script');
            Array.prototype.forEach.call(old.attributes, function (a) {
                if (a.name !== 'type' && a.name !== 'data-wpide-consent') { s.setAttribute(a.name, a.value); }
            });
            if (!old.src) { s.text = old.text; }
            old.parentNode.replaceChild(s, old);
        });
    }
    release();
    document.addEventListener('wp_listen_for_consent_change', release);
    document.addEventListener('wpide:consent', release);
})();
</script>
    <?php
}, 99 );

Notes

Find script handles with a "list enqueued assets" snippet; scripts that other scripts depend on should not be held back.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.