# Reddit Pixel

> Adds the Reddit Ads pixel to every public page and tracks page visits, with an option to wait for cookie consent.

- Type: PHP
- Runs: Frontend only
- Category: [Analytics & Tracking](https://wpide.com/library/category/analytics/)
- Tags: reddit, pixel, ads, tracking
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/analytics-reddit-pixel/

## Code

```php
add_action( 'wp_head', function () {
    $pixel_id        = 'YOUR_PIXEL_ID'; // Reddit pixel ID.
    $require_consent = false;           // true: wait for consent from the WPIDE cookie banner.

    if ( 'YOUR_PIXEL_ID' === $pixel_id || current_user_can( 'edit_posts' ) ) {
        return;
    }
    ?>
<script>
(function (run) {
    if (!<?php echo $require_consent ? 'true' : 'false'; ?> || /(?:^|; )wpide_consent=granted/.test(document.cookie)) { run(); return; }
    document.addEventListener('wpide:consent', function (e) { if (e.detail === 'granted') { run(); } });
})(function () {
    if (!window.rdt) {
        var p = window.rdt = function () { p.sendEvent ? p.sendEvent.apply(p, arguments) : p.callQueue.push(arguments); };
        p.callQueue = [];
        var s = document.createElement('script');
        s.async = true;
        s.src = 'https://www.redditstatic.com/ads/pixel.js';
        document.head.appendChild(s);
    }
    rdt('init', <?php echo wp_json_encode( $pixel_id ); ?>);
    rdt('track', 'PageVisit');
});
</script>
    <?php
} );
```

## Notes

Replace YOUR_PIXEL_ID with your Reddit pixel ID (it starts with a2_ or t2_). Nothing is output while the placeholder is in place.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Reddit Pixel" 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/
