Php code right after <body> GTM script

I use Metronet tag manager plugin to implement the GTM script.

But to make it load imminently and track correctly I need to add a row after the <body> where is that located in PRO? And since this will fall back to default on every theme update I prefer both parent and child solutions.

My guess is that it’s the base.php? VIEWS/HEADER/BASE.PHP

<html class="no-js" <?php language_attributes(); ?>>

<head>
  <?php wp_head(); ?>
</head>

<body <?php body_class(); ?>>
<?php do_action( 'body_open' ); ?>
  <div <?php echo $x_root_atts; ?>>

Plugin: https://wordpress.org/plugins/metronet-tag-manager/

Please note that for this plugin to work there is a slight customization that is needed. WordPress doesn’t let you load scripts straight after the opening tag which is where the GTM script needs to be placed to work correctly. To fix this you need to add <?php do_action( 'body_open' ); ?> just after the tag and that’s it.

Hi There,

Thanks for writing in! Please follow the code below and add it into your child theme’s functions.php file.

// Add PHP Code after body tag

function my_custom_code() { 
  // Add your custom code here
}

add_action('x_before_site_begin', 'my_custom_code');

Hope that helps.

1 Like

Hi @mldarshana

Thanks for the reply. I hope it helps, it’s hard to track if it’s going to work s it should. Since I do need to have a third-party analytics to see if Google Analytics is missing anything. I just want to make sure it tracks correct with the GTM scripts, so follow what the plugin developer suggest is a good idea.

So according to you that code row in my functions.php file is all I need to do?

Or do you mean I should add <?php do_action( 'body_open' ); ?> where // Add your custom code here is? Like following rows:

// Add PHP Code after body tag

function my_custom_code() { 
 <?php do_action( 'body_open' ); ?>
}

add_action('x_before_site_begin', 'my_custom_code');

Hi there,

The hook which we suggested (x_before_site_begin) is just right after the body_open which you can use in this case. You can find the hook in line 27 of the file below:

wp-content/themes/x/framework/views/header/base.php

That is the standard way of adding extra scripts to our theme using the hook we already mentioned. You do not need to add the body_open one.

Just add the code to the section mentioned in suggested script and it should work with no problem.

Thank you.

I see, thanks for the clarification here.

I assume it doesn’t matter where in my child functions.php I paste the rows, top or bottom? My function.php has a few customizations.

thanks

Hi there,

You can place the code anywhere in the functions.php as long as there is no incorrect syntax in the code. For it to be cleaner, you can add it in the bottom of the file.

Why are we now editing headers in our functions.php? What if I have multiple header requirements that I don’t want globally hooked? How do we do it old school with wp_header?

Hi @bluedotproductions,

Thanks for writing around! Regretfully, at this time I am not entirely certain what it is you would like to accomplish based on the information given in your email. If you wouldn’t mind providing us with a little more clarification on what it is you’re wanting to do (a link to a similar example site would be very helpful, or perhaps some screenshots), we’ll be happy to provide you with a response once we have a better understanding of the situation.

I am asking how to edit the headers like we did in version 4 with 5. I have 30 custom headers. Here’s a screen grab of my child theme in my renew stack that wants to be converted to v5 with your new header system, not requiring your “legacy” automatic fallback.

Hi there @bluedotproductions,

In your case, you’d need to specify in your header file for the individual header you wish to modify the output using the wp_head hook https://codex.wordpress.org/Plugin_API/Action_Reference/wp_head which should work perfectly for your needs.

If you get stuck or need any help, please open a new thread and someone will be right along to assist.

Thanks!