• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Adding code into the Genesis (child theme) header

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › General Genesis Framework Discussions › Adding code into the Genesis (child theme) header

This topic is: not resolved

Tagged: functions.php, google tag manager, wp_head

  • This topic has 10 replies, 3 voices, and was last updated 1 year, 2 months ago by Anita.
Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • March 15, 2021 at 6:22 am #503459
    endolil
    Participant

    Hi there,

    I am trying to add code into the theme header, as high as possible.
    It's a snippet we got from the Google Tag Manager.

    Does anybody have a recommendation how I can do this in the most elegant way?
    I am using Advanced Custom Fields and I have set up an Options page to work with specific fields that can be used in the theme.

    So for example:

    // get code for the header 
    $gtm_header_code = get_field('google_tagemanager_code', 'option');
    
    // check if activated
    $gtm_header_code_active = get_field('google_tagmanager_code_active', 'option');
    
    // if code exists and is activated
    if($gtm_header_code_active == TRUE && $gtm_header_code) :
    		echo $gtm_header_code;
    endif;

    Now I would like to make sure that I can output this code right after the <head> tag.

    I have tried using add_action( 'wp_head' ....
    but it does not allow me to get high up into the <head> tag

    AND

    I will also need to add code right after the opening <body> tag.
    I have not found any hook that allows me to do that!
    Can anybody suggest a good way how to utilize the functions.php to add these codes where I need them to be?
    Even a partial solution (for one of my two tasks) would be helpful!

    March 15, 2021 at 6:36 am #503461
    Victor Font
    Moderator

    The genesis_before action hook should fire right after the opening body tag.

    As for wp_head, run it with a lower priority so it fires earlier.


    Regards,

    Victor
    https://victorfont.com/
    Call us toll free: 844-VIC-FONT (842-3668)
    Have you requested your free website audit yet?

    March 15, 2021 at 6:49 am #503463
    endolil
    Participant

    Thank you, Victor.

    the lowest priority would be 0, right?

    add_action( 'wp_head', 'my_script', 0 );

    So genesis_before is not depreciated???

    And when I add this to my functions.php (child theme), nothing happens:

    function gtm_body_code() {

    // get code for the body
    $gtm_body_code = get_field('google_code_body', 'option');

    // check if activated
    $gtm_body_code_active = get_field('google_code_body_active', 'option');

    // if code exists and is activated
    if($gtm_body_code_active == TRUE && $gtm_body_code) :
    echo $gtm_body_code;
    endif;

    echo "body code"; // just to test

    }
    add_action( 'genesis_before', 'gtm_body_code' );`

    Any idea why this is (not) happening?

    March 15, 2021 at 8:52 am #503465
    Victor Font
    Moderator

    I think 1 is the lowest priority for an action. Take a look at the wp_head hook documentation. wp_head uses many priorities: https://developer.wordpress.org/reference/hooks/wp_head/

    And truthfully, the Google tag manager code probably doesn't have to be all the way at the top of the header. I've seen work no matter where it is in the header.

    The default for most modern browsers is to block Google tracking scripts these days. Check your console to make sure the browser isn't blocking your scripts.


    Regards,

    Victor
    https://victorfont.com/
    Call us toll free: 844-VIC-FONT (842-3668)
    Have you requested your free website audit yet?

    March 16, 2021 at 12:32 am #503481
    endolil
    Participant

    Thank you so much!
    I am going to try to get before_genesis to run and I already managed to use the wp_head function to add the code to the header. And yes, I do think it's fine if it is not very high up in the html head tag.

    And the blocking of scripts is probably something our marketing team will look into as I am only adding the code for now and have nothing to do with the actual tracking and analysis.

    Thanks for mentioning it!

    March 16, 2021 at 1:20 am #503482
    endolil
    Participant

    I am still struggling with adding the code to the body:

    // additional code that goes into the document body 
    	// in this case it is the Google Tag Manager noscript code
    	
    	function gtm_body_code() {
    
    		// get code for the body 
    		$gtm_body_code = get_field('google_code_body', 'option');
    
    		// check if activated
    		$gtm_body_code_active = get_field('google_code_body_active', 'option');
    
    		// if code exists and is activated
    		if($gtm_body_code_active == TRUE && $gtm_body_code) :
    			echo $gtm_body_code;
    		endif;
    
    	}
    
    	add_action( 'before_genesis', 'gtm_body_code' );
    	add_action( 'wp_body_open', 'gtm_body_code' );

    Both actions don't seem to trigger that the code is being added.
    I have this in my functions.php

    Any idea why this does not work??

    March 17, 2021 at 9:35 am #503498
    Anita
    Keymaster

    Take a look at the Visual Hook Guide for Genesis. You have before_genesis which I believe should be genesis_before.


    Love coffee, chocolate and my Bella!

    March 19, 2021 at 2:55 am #503514
    endolil
    Participant

    That was a good hint, Anita! Thank you!

    For some reason I can't get any output when I use

    add_action('genesis_before', 'my_code');
    or
    add_action( 'wp_body_open', 'my_code' );

    so I ended up using this
    add_action( 'wp_footer', 'my_code' );

    to add a noscript tag from the google tag manager into the page.
    It's not ideal but it works.
    I have no idea why the other "add_actions" don't work in my child theme. It's mysterious. I never had this problem before.

    March 19, 2021 at 8:38 am #503517
    Anita
    Keymaster

    Are you trying to add a script just to one page or to multiple pages?


    Love coffee, chocolate and my Bella!

    March 27, 2021 at 5:06 am #503589
    endolil
    Participant

    multiple pages 😉

    March 27, 2021 at 10:34 am #503590
    Anita
    Keymaster

    You can use Genesis Simple Hooks to do that. Even though it hasn't been updated for the last three versions, it still works. An updated may not be required. I would use that first to find the correct hook. Have you used that before? There is also the Header and Footer Scripts box under Genesis > Theme Settings where you can add that script without needing to add code.

    Sorry I can't be of more help with the actual code. Someone in the Facebook group might be able to help with your code.


    Love coffee, chocolate and my Bella!

  • Author
    Posts
Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.
Log In

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2022 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble