Trying to add google code to header / body

I’m trying to add google tag manager to a website running pro and code needs to be added to part of the header and the body, so I looked on the forums to find where to add code and it seems it needs to be added in base.php

If I grab the file:
pro/framework/views/header/base.php

from the theme folder and copy/paste it into the child theme area:

pro-child/framework/views/header/base.php

before even adding any code from google tag manager and leaving it just as it was in the main theme area, the whole header and menu disappears from the website.

Can you give me any suggestions on what I’m doing wrong?

This is the code contained in the file:

<?php

// =============================================================================
// VIEWS/HEADER/BASE.PHP
// -----------------------------------------------------------------------------
// Declares the DOCTYPE for the site, includes the <head>, opens the <body>
// element as well as the .x-root <div> and .x-site <div>.
// =============================================================================

$x_root_atts = x_atts( apply_filters( 'x_root_atts', array( 'id' => 'x-root', 'class' => 'x-root' ) ) );
$x_site_atts = x_atts( apply_filters( 'x_site_atts', array( 'id' => 'x-site', 'class' => 'x-site site' ) ) );

?>
<!DOCTYPE html>

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

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

<body <?php body_class(); ?>>

  <?php do_action( 'x_after_body_begin' ); ?>

  <div <?php echo $x_root_atts; ?>>

    <?php do_action( 'x_before_site_begin' ); ?>

    <div <?php echo $x_site_atts; ?>>

    <?php do_action( 'x_after_site_begin' ); ?>

Hey @Boans,

Though you can add codes in the head by overriding the base.php file, that is not recommended. The recommended way is using the WordPress wp_head hook. Add the following code in your child theme’s functions.php and replace the PASTE HEADER CODE HERE text and also the function name.

/* Describe what the code snippet does so you can remember later on */
add_action('wp_head', 'your_function_name');
function your_function_name(){
?>
PASTE HEADER CODE HERE
<?php
};

Please just bear in mind that though it’s just copy and paste, just missing one character in that code can break your site and we do not support issues arising from the use of custom code.

If you’re not comfortable working with code, there are plugins available that can inject code to the head. For more details, please check this article: https://kinsta.com/knowledgebase/add-code-wordpress-header-footer/

Hope that helps.

Thanks! I will do that, but part of adding the google tag manager code is to add 1 part to the head and 1 part to the body right after the body tag. Is there one I can use to place the second part of the code right after the body tag? … I tried wp_body and it didn’t work. (The part you sent did work though … with wp_head)

Hello @Boans,

There is no wp_body filter in Wordpress that is why it is not working.

You can do it this way instead:

// Add code after body tag in X/Pro themes

add_action('x_after_body_begin', 'custom_code_after_open_body');

function custom_code_after_open_body() {
  ?>
  <!-- Custom Code Goes Here -->
  <?php
}

Hope this helps.

Perfect! Thanks!

We are delighted to assist you with this.

Cheers!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.