Viewing 8 posts - 1 through 8 (of 8 total)
  • Posted in: Uplift
  • #295364
    micheal_w
    Member
    Post count: 498

    Hi guys,

    would like to add the new Facebook pixel to my site without any plugin.
    I am using your child theme, but there is not header.php file in there where I could include the pixel code.

    Can you please advise how to / what to add to the child theme to be able to have a header.php file (or similar)?
    Thanks

    #295526
    Rui Guerreiro – SUPPORT
    Keymaster
    Post count: 25779

    Hi,

    Never tried Facebook Pixel but try to add the code below to the functions.php of your child theme.

    add_action('wp_head', 'facebook_pixel_wp_head');
    function facebook_pixel_wp_head(){
        //Close PHP tags 
        ?>
    
      <!-- 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','//connect.facebook.net/en_US/fbevents.js');
    // Insert Your Facebook Pixel ID below. 
    fbq('init', 'FB_PIXEL_ID');
    fbq('track', 'PageView');
    </script>
    <!-- Insert Your Facebook Pixel ID below. --> 
    <noscript><img height="1" width="1" style="display:none"
    src="https://www.facebook.com/tr?id=FB_PIXEL_ID&ev=PageView&noscript=1"
    /></noscript>
    <!-- End Facebook Pixel Code -->
    
        <?php //Open PHP tags
    }

    this is just an example replace the code inside by your facebook pixel code.

    Let us know if it worked.

    -Rui

    #295637
    micheal_w
    Member
    Post count: 498

    Hi Rui,

    I am comfortable with the FB Pixel, I would just need one thing:
    – THE FB pixes should be place in the header.php file just before the close </head> tag.

    However I don’t have a header.php file in my child theme and don’t know how to create one.
    Can you please put me in the right direction?

    #295650
    David Martin – Support
    Moderator
    Post count: 20834

    The function above is better then adding it to an actual file, the code above will automatically add the code using the WordPress action wp_head with is called within the header.php file. So it saves you keeping the child theme template file updating with each release.

    #295702
    micheal_w
    Member
    Post count: 498

    Hi David,

    okay, thanks I will try tonight.

    Just one question:
    I thought that if I put any custom stuff in the child theme, I don’t need to worry about anything when updating the themes…
    I thought the child theme is for this reason… Or am I wrong or just missing something?

    #295705
    Rui Guerreiro – SUPPORT
    Keymaster
    Post count: 25779

    @micheal_w

    Lot’s of people have the perception that using a child theme they don’t need to worry with updates and that is not 100% true because if we change a file that you also customize things can break if you don’t copy the new changes across.

    The function I provided will add the code to the head of the page(that’s what FB asks), so give it a try.

    -Rui

    #296711
    micheal_w
    Member
    Post count: 498
    This reply has been marked as private.
    #297164
    David Martin – Support
    Moderator
    Post count: 20834

    You would create new new functions using the template code Rui provided. An example is below, you then add your code to the two new functions.

    add_action('wp_head', 'gtm_pixel_wp_head');
    function gtm_pixel_wp_head(){ ?>
    
    <!-- ADD YOUR CODE HERE -->
    
    <?php 
    }
    add_action('sf_before_page_container', 'gtm_noscript_pixel_wp_head');
    function gtm_noscript_pixel_wp_head(){ ?>
    
    <!-- ADD YOUR CODE HERE -->
    
    <?php 
    }

    Thanks,
    David.

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.