2

I'm using the code below to do the following task:

If page id is 22834 load a specific facebook pixel script with "Purchased" track event, else (all other pages) load the facebook pixel script with "PageView" track event.

Current code:

function wpb_hook_javascript() {
  if (is_page ('22834')) { 
    ?>
        <script>!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', 'XXXXXXXXXX');
        fbq('track', 'Purchase');</script><noscript><img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=XXXXXXXXXX&ev=Purchase&noscript=1"></noscript>
    
<?php else ?>

        <script>!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', 'XXXXXXXXXX');
        fbq('track', 'PageView');</script><noscript><img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=XXXXXXXXXX&ev=PageView&noscript=1"></noscript>

<?php
  }
}

add_action('wp_head', 'wpb_hook_javascript');

The script is working and applying the code correctly in my website, but with this code in my functions.php the wp-admin area it's showing this error:

There has been a critical error on your website. Please check your site admin email inbox for instructions.

If I remove the code the wp-admin works again!

1
  • 2
    "There has been a critical error on your website." . Please check your logs and post the full error message Commented Jan 7, 2022 at 14:33

1 Answer 1

2
  1. It's recommended to use wp_enqueue_scripts instead of using wp_head hook for outputing js and css.

  2. You may need to use !is_admin() condition inside your function as well.

  3. You're missing a closing } and opening { before and after your else statement.

function wpb_hook_javascript(){
    if (!is_admin() && is_page('22834')) {
    ?>
        <script>
            ! 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', 'XXXXXXXXXX');
            fbq('track', 'Purchase');
        </script><noscript><img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=XXXXXXXXXX&ev=Purchase&noscript=1"></noscript>

    <?php } else { ?>

        <script>
            ! 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', 'XXXXXXXXXX');
            fbq('track', 'PageView');
        </script><noscript><img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=XXXXXXXXXX&ev=PageView&noscript=1"></noscript>

<?php
    }
}

add_action('wp_head', 'wpb_hook_javascript');
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for help me on this problem :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.