GTM code inside functions.php based on WordPress

I try to add code inside functions.php for a GTM tracking.
How to add code as t should be added immediately after the opening tag.

 <head></head><body><!-- GTM -->
function my_injects()
{
	wp_enqueue_script('gtm_1', get_template_directory_uri() . '/js/gtm/gtm1.js', '', '', true);
}

If you are using functions.php file, it depends on the hooks in the theme you are using. Which theme are you using?

I will manually add inside a custom theme GTM without a plugin. How to connect a hook inside functions.php and PHP code inside a theme (PHP format)?

To inject the Google Tag Manager (GTM) code into your WordPress theme directly without using a plugin, you’ll want to add the code immediately after the opening <body> tag.

Follow these steps

The GTM code usually comes in two parts: a script that goes into the <head> and a noscript section that goes immediately after the opening <body> tag. Make sure you have both parts handy.

Then, add the following code to your theme’s functions.php

// Add GTM code after <body>
function inject_gtm_body() {
    ?>
    <!-- Google Tag Manager (noscript) -->
    <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=YOUR-GTM-ID"
    height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
    <!-- End Google Tag Manager (noscript) -->
    <?php
}
add_action('wp_body_open', 'inject_gtm_body');

// Add GTM code to <head>
function inject_gtm_head() {
    ?>
    <!-- Google Tag Manager -->
   
    <!-- End Google Tag Manager -->
    <?php
}
add_action('wp_head', 'inject_gtm_head');

Ensure the wp_body_open Hook Exists - The wp_body_open hook should be located right after the opening <body> tag in your theme. If your theme doesn’t have this hook, you’ll need to add it.

Now open your theme’s header.php file and ensure the following code exists right after the <body> tag

<?php
if (function_exists('wp_body_open')) {
    wp_body_open();
}
?>

the script will execute any actions attached to the wp_body_open hook, including the GTM noscript injection.

1 Like

I have tried the following code inside functions.php

function google_gtm_body() {
?>
TESTGTMBODY<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-ID" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<?php
}
add_action('wp_body_container', 'google_gtm_body');

function google_gtm_head() {
?>
GTM tracking code<!-- Google Tag Manager --><!-- End Google Tag Manager -->
<?php
}
add_action('wp_head_container', 'google_gtm_head');

and inside body.php:

<?php if (function_exists('wp_body_container')) {wp_body_container();} ?>TESTBODYCONTAINER

and inside header.php:

<?php if (function_exists('wp_head_container')) {wp_head_container();} ?>TESTHEADERCONTAINER

Do you know if something is wrong as TESTGTMBODY and GTM tracking code is not pushed.

Did I missed something?

Let me see…
Invalid Action Hooks:

  • The wp_body_container and wp_head_container hooks don’t exist in the core WordPress by default. The typical hooks for these areas are wp_body_open (right after the opening <body> tag) and wp_head (within the <head> section).

Try this;
change your hooks in functions.php

function google_gtm_body() {
?>
TESTGTMBODY<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-ID" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<?php
}
add_action('wp_body_open', 'google_gtm_body');

function google_gtm_head() {
?>
GTM tracking code<!-- Google Tag Manager --><!-- End Google Tag Manager -->
<?php
}
add_action('wp_head', 'google_gtm_head');

Update body.php - Make sure you have the wp_body_open action call right after the opening <body> tag:

<body>
<?php if (function_exists('wp_body_open')) { wp_body_open(); } ?>TESTBODYCONTAINER

Update header.php - Make sure you have the wp_head action call within the <head> section:

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

Your code should work by these changes, and the GTM snippets should appear where you want them to.

Thank you for the message. It works.

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