# Cookie Consent Banner With Google Consent Mode

> Shows a simple Accept / Reject cookie banner, sets Google Consent Mode v2 defaults to "denied" until the visitor accepts, and remembers the choice for six months.

- Type: PHP
- Runs: Frontend only
- Category: [Analytics & Tracking](https://wpide.com/library/category/analytics/)
- Tags: consent, cookies, gdpr, google
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/analytics-consent-banner/

## Code

```php
$wpide_consent = array(
    'text'       => 'We use cookies to understand how our site is used and to improve your experience.',
    'policy_url' => get_privacy_policy_url(), // Link to your privacy policy.
    'days'       => 180,                       // How long the choice is remembered.
);

add_action( 'wp_head', function () {
    ?>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
(function () {
    var granted = /(?:^|; )wpide_consent=granted/.test(document.cookie);
    var state = granted ? 'granted' : 'denied';
    gtag('consent', 'default', { ad_storage: state, ad_user_data: state, ad_personalization: state, analytics_storage: state, wait_for_update: 500 });
})();
</script>
    <?php
}, 1 );

add_action( 'wp_footer', function () use ( $wpide_consent ) {
    ?>
<div id="wpide-consent" role="dialog" aria-live="polite" aria-label="Cookie consent" hidden style="position:fixed;left:16px;right:16px;bottom:16px;z-index:99999;max-width:640px;margin:0 auto;padding:16px 20px;border-radius:8px;background:#1e1e1e;color:#fff;box-shadow:0 4px 20px rgba(0,0,0,.25);font-size:14px;line-height:1.5;">
    <p style="margin:0 0 12px;"><?php echo esc_html( $wpide_consent['text'] ); ?>
    <?php if ( $wpide_consent['policy_url'] ) : ?>
        <a href="<?php echo esc_url( $wpide_consent['policy_url'] ); ?>" style="color:#fff;text-decoration:underline;">Privacy policy</a>
    <?php endif; ?></p>
    <button type="button" data-consent="granted" style="margin-right:8px;padding:8px 16px;border:0;border-radius:4px;background:#fff;color:#1e1e1e;cursor:pointer;">Accept</button>
    <button type="button" data-consent="denied" style="padding:8px 16px;border:1px solid #fff;border-radius:4px;background:transparent;color:#fff;cursor:pointer;">Reject</button>
</div>
<script>
(function () {
    var box = document.getElementById('wpide-consent');
    if (!box) { return; }
    if (!/(?:^|; )wpide_consent=/.test(document.cookie)) { box.hidden = false; }
    box.addEventListener('click', function (e) {
        var choice = e.target.getAttribute('data-consent');
        if (!choice) { return; }
        var days = <?php echo (int) $wpide_consent['days']; ?>;
        document.cookie = 'wpide_consent=' + choice + ';path=/;max-age=' + (days * 86400) + ';SameSite=Lax' + (location.protocol === 'https:' ? ';Secure' : '');
        if (typeof gtag === 'function') {
            gtag('consent', 'update', { ad_storage: choice, ad_user_data: choice, ad_personalization: choice, analytics_storage: choice });
        }
        document.dispatchEvent(new CustomEvent('wpide:consent', { detail: choice }));
        box.hidden = true;
    });
})();
</script>
    <?php
} );
```

## Notes

A basic banner, not a full consent platform; WPIDE pixel snippets wait for it when their $require_consent is set to true.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Cookie Consent Banner With Google Consent Mode" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
