Skip to content

Instantly share code, notes, and snippets.

@nathaningram
Created March 31, 2026 17:55
Show Gist options
  • Select an option

  • Save nathaningram/a6defc0ad48a71149572dbc8019bed8e to your computer and use it in GitHub Desktop.

Select an option

Save nathaningram/a6defc0ad48a71149572dbc8019bed8e to your computer and use it in GitHub Desktop.
WooCommerce Pinterest Pixel Fix for Termageddon / Usercentrics Cookie Consent Tool
// Fix: Pinterest for WooCommerce pixel + UserCentrics consent
//
// The plugin outputs the Pinterest base code via echo in wp_footer — there's
// no filter to modify it. This snippet uses output buffering to intercept the
// full page HTML and replace the script type/attributes so UserCentrics can
// manage consent for the Pinterest pixel.
//
// What it does: changes <script type="text/javascript"> to
// <script type="text/plain" data-usercentrics="Pinterest"> inside the
// Pinterest Pixel Base Code block only. This targets both the standard and
// enhanced-match variants of the tag.
//
// Update-safe: the replacement key is the plugin's own HTML comment marker,
// which is a hardcoded constant in Tag.php. If the marker changes in a future
// plugin update, the fix silently stops applying (pixel reverts to default
// behavior) rather than breaking the page.
function bww_usercentrics_pinterest_pixel_fix( $buffer ) {
return str_replace(
"<!-- Pinterest Pixel Base Code -->\n<script type=\"text/javascript\">",
"<!-- Pinterest Pixel Base Code -->\n<script type=\"text/plain\" data-usercentrics=\"Pinterest\">",
$buffer
);
}
add_action( 'template_redirect', function() {
if ( ! function_exists( 'Pinterest_For_Woocommerce' ) ) {
return;
}
ob_start( 'bww_usercentrics_pinterest_pixel_fix' );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment