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.
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.
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: