Community Forums › Forums › General Genesis Framework Discussions › Adding code into the Genesis (child theme) header
Tagged: functions.php, google tag manager, wp_head
- This topic has 10 replies, 3 voices, and was last updated 3 years, 6 months ago by AnitaC.
-
AuthorPosts
-
March 15, 2021 at 6:22 am #503459endolilParticipant
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> tagAND
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 #503461Victor FontModeratorThe 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 #503463endolilParticipantThank 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 #503465Victor FontModeratorI 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 #503481endolilParticipantThank 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 #503482endolilParticipantI 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.phpAny idea why this does not work??
March 17, 2021 at 9:35 am #503498AnitaCKeymasterTake a look at the Visual Hook Guide for Genesis. You have
before_genesis
which I believe should begenesis_before
.
Need help with customization or troubleshooting? Reach out to me.
March 19, 2021 at 2:55 am #503514endolilParticipantThat 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 #503517AnitaCKeymasterAre you trying to add a script just to one page or to multiple pages?
Need help with customization or troubleshooting? Reach out to me.
March 27, 2021 at 5:06 am #503589endolilParticipantmultiple pages 😉
March 27, 2021 at 10:34 am #503590AnitaCKeymasterYou 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.
Need help with customization or troubleshooting? Reach out to me.
-
AuthorPosts
- You must be logged in to reply to this topic.