0

I am trying to add the following Google Tag Manager script immediately after the opening <body> tag on the Wordpress login page.

Code that I want to insert

<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXXXX"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->

What I have tried

Based on the solution given here: Adding body class to login page?, I tried the following code:

function my_custom_code2($classes){
?>
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-P3CWDSQ"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->";
<?php 
}

add_filter( 'login_body_class', 'my_custom_code2' );

Results

It does not work. Does anyone know how I can fix this?

In response to Sally's query below, this is the full code I am using.

function my_custom_code1(){
?>
 
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXX');</script>
<!-- End Google Tag Manager -->

<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXXX"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->";

<?php 
}
add_action( 'login_head', 'my_custom_code1', 10 );

Thanks.

1

immediately after the opening <body> tag

WordPress doesn't provide a hook for that, i.e. for adding code exactly after the <body> tag.

But WordPress does fire a hook named login_header which we can use to add code after the body tag is opened:

do_action( 'login_header' )
Fires in the login page header after the body tag is opened.

So for example:

add_action( 'login_header', 'wpse_375598' );
function wpse_375598() {
    ?>
        Add your HTML here.
    <?php
}

And BTW, login_body_class is a filter hook one can use to add custom CSS classes into the <body> tag (i.e. <body class="here">), so that hook should not be used to add the (Google Tag Manager's) noscript code.

Also in the edited question, the problem is that you used login_head and not login_headerlogin_head is for adding code in the <head> section of the document/page.

So your code should look more like:

add_action( 'login_head', 'wpse_375598_login_head' );
function wpse_375598_login_head() {
    ?>
        Add your HTML here that goes in the <head>.
    <?php
}

add_action( 'login_header', 'wpse_375598_login_header' );
function wpse_375598_login_header() {
    ?>
        Add your HTML here that goes in the <body>.
    <?php
}
  • Thanks Sally, but doesn't that add items to the header instead of the body? I tried your method and I get this error message for Google Tag Manager "GTM no-script code not found: either missing or not in <body>". – xojijog684 Sep 29 '20 at 5:47
  • The docs says, "Fires in the login page header after the body tag is opened.", so the hook does fire in the document body. However, if you mean exactly after the <body> tag, then I'm afraid there's no hook available for that. – Sally CJ Sep 29 '20 at 5:53
  • Thanks for helping me out Sally. Would you mind having a look at my site: rebrand.ly/sallycj It still shows that the code is being deployed before the body tag (i.imgur.com/ByA0ddi.png) – xojijog684 Sep 29 '20 at 6:00
  • Can you show the PHP code you used? Add it in the question or put it on Pastebin.com. – Sally CJ Sep 29 '20 at 6:16
  • 1
    Thanks Sally! It worked :) – xojijog684 Sep 29 '20 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.