1

I've been trying to figure out what I could possibly be doing wrong with this plugin I just coded, from what I see, everything should be working correctly. However, it's not doing anything at all. No errors though. Any ideas?

Here's facebook-pixel.php

<?php
/*
Plugin Name: Facebook Pixel
Plugin URI: N/A
Description: A facebook pixel plugin coded to optimize conversions from facebook ads
Version: 1
Author: Joseph Roberts
Author URI: http://josephrobertsdesigns.com
License: GPL2
*/


// Make sure we don't expose any info if called directly
if ( !function_exists( 'add_action' ) ) {
    echo 'Hi there!  I\'m just a plugin, not much I can do when called directly.';
    exit;
}

define( 'FACEBOOK_PIXEL__VERSION', '1' );
define( 'FACEBOOK_PIXEL__MINIMUM_WP_VERSION', '4.7.2' );
define( 'FACEBOOK_PIXEL__PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'FACEBOOK_PIXEL__DELETE_LIMIT', 100000 );

register_activation_hook( __FILE__, array( 'Facebook_Pixel', 'plugin_activation' ) );
register_deactivation_hook( __FILE__, array( 'Facebook_Pixel', 'plugin_deactivation' ) );

require_once( FACEBOOK_PIXEL__PLUGIN_DIR . 'facebook-pixel-run.php' );

And here's facebook-pixel-run.php

<?php

function facebook_pixel_header(){
    ob_start();
    ?>
        <!-- Facebook Pixel Code -->
        <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', 'My_Facebook_Pixel_ID', {
        em: 'insert_email_variable,'
        });
        fbq('track', 'PageView');
        </script>
        <noscript><img height="1" width="1" style="display:none"
        src="https://www.facebook.com/tr?id=1128567750605621&ev=PageView&noscript=1"
        /></noscript>
        <!-- DO NOT MODIFY -->
        <!-- End Facebook Pixel Code -->
    <?
    if( is_page('Exhibit Here') ){
        if( isset($_POST['submit']) && !isset($error) ){
            if($_POST['booth-type']=="for-profit"){ $total = 620; }else{ $total = 565; }
            if($_POST['booth-number']=="2"){
                $total = $total*2;
            }elseif($_POST['booth-number']=="3"){
                $total = $total*3;
            }elseif($_POST['booth-number']=="4"){
                $total = $total*4;
            }elseif($_POST['booth-number']=="5"){
                $total = $total*5;
            }elseif($_POST['booth-number']=="6"){
                $total = $total*6;
            }
            ?>
                <!-- The below method uses jQuery, but that is not required -->
                <button id="addToCartButton">Add To Cart</button>
                <!-- Add event to the button's click handler -->
                <script type="text/javascript">
                $( '#addToCartButton' ).click(function() {
                fbq('track', 'AddToCart', {
                content_ids: ['1234'],
                content_type: 'product',
                value: <?php echo number_format($total, 2, '.', ''); ?>,
                currency: 'USD'
                });
                });
                </script>
                <script>
                fbq('track', 'AddPaymentInfo');
                </script>
                <script>
                fbq('track', 'CompleteRegistration', {
                value: <?php echo number_format($total, 2, '.', ''); ?>,
                currency: 'USD'
                });
                </script>
            <?
        }else{
            ?>
            <script>
            fbq('track', 'InitiateCheckout');
            </script>
            <?
        }
    }
    ob_end_clean();
}

add_action('wp_head', 'facebook_pixel_header');

Any help would be greatly appreciated. Thank you.

4
  • review the function ob_end_clean() – Michael Mar 1 '17 at 21:31
  • @Michael What do you mean review? Just for clarification I did add the ob_start(); and the ob_end_clean(); because it still wasn't outputting anything without them and I thought it may cause it to work. (Really just a shot in the dark) – J Robz Mar 1 '17 at 21:43
  • ob_end_clean() does not output anything; just check online tutorials for what php output buffering does. not a WordPress problem. – Michael Mar 1 '17 at 22:13
  • wp_head action filter corresponds wp_head() function call and it's used just before the HTML </head> tag. So whatever HTML it produces (without using the ob_start() and ob_end_clean()), it'll not work as expected. And with using ob_end_clean() that way is just wrong. – Fayaz Mar 1 '17 at 23:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.