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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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_header — login_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
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0