Last active
August 11, 2025 04:38
-
-
Save westonruter/1801e111ab66cd6b0cac8dab05782430 to your computer and use it in GitHub Desktop.
How to adapt the Base Code for Facebook Pixel to be compatible with Strict CSP: https://developers.facebook.com/docs/meta-pixel/get-started/#base-code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// This adaptation of the Base Code for Facebook Pixel is compatible with the Strict CSP plugin: https://wordpress.org/plugins/strict-csp/ | |
add_action( | |
'wp_head', | |
static function () { | |
?> | |
<!-- Facebook Pixel Code --> | |
<?php | |
$js = <<<JS | |
!function(f,b,e,v,n,t,s) | |
{if(f.fbq)return;n=f.fbq=function(){n.callMethod? | |
n.callMethod.apply(n,arguments):n.queue.push(arguments)}; | |
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0'; | |
n.queue=[];t=b.createElement(e);t.async=!0; | |
t.src=v;s=b.getElementsByTagName(e)[0]; | |
s.parentNode.insertBefore(t,s)}(window, document,'script', | |
'https://connect.facebook.net/en_US/fbevents.js'); | |
fbq('init', '{your-pixel-id-goes-here}'); | |
fbq('track', 'PageView'); | |
JS; | |
wp_print_inline_script_tag( $js ); | |
?> | |
<noscript> | |
<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id={your-pixel-id-goes-here}&ev=PageView&noscript=1"/> | |
</noscript> | |
<!-- End Facebook Pixel Code --> | |
<?php | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment