X (Twitter) Pixel
Adds the X (formerly Twitter) website tag to every public page for conversion tracking and audiences, with an option to wait for cookie consent.
Opens WPIDE on
Code
add_action( 'wp_head', function () {
$pixel_id = 'YOUR_PIXEL_ID'; // X 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.twq) {
var q = window.twq = function () { q.exe ? q.exe.apply(q, arguments) : q.queue.push(arguments); };
q.version = '1.1';
q.queue = [];
var s = document.createElement('script');
s.async = true;
s.src = 'https://static.ads-twitter.com/uwt.js';
document.head.appendChild(s);
}
twq('config', <?php echo wp_json_encode( $pixel_id ); ?>);
});
</script>
<?php
} );
Notes
Replace YOUR_PIXEL_ID with the pixel ID from X Ads > Tools > Events Manager. Nothing is output while the placeholder is in place.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.