Hey,
Thanks for posting.
If you truly want in the “header” then a plugin like this would work: https://wordpress.org/plugins/header-and-footer-scripts-inserter/
But most people want to add google tag manager right after the opening body tag. For that in a child theme you can use this function in the functions.php file of your child theme:
add_action('virtue_after_body', 'custom_add_google_tag_code');
function custom_add_google_tag_code() {
echo 'script goes here';
}
Finally, it’s worth noting that this is a built-in option with the premium theme.
Ben
Hi Ben and thank you for your answer!
Yeah, I will consider using the premium version in the future, but now I want to start with the free version.
Well, I tried to put the function you wrote above in the functions.php file in the child theme, but it gave me all the time “missing_content” and it did not allow me to modify the functions.php file.
Otherwise the plugin “Head and Footer Scripts Inserter” fires well without actually the need to use the function in the functions.php file, but it puts the second GTM code after the </footer> tag. Do you think it’s OK to be like that, or I can maybe have another solution?
Thank you for your assistance in advance
Kind Regards,
Alex
Can you show here exactly how your functions.php file looked?
Ben
I don’t suggest that footer being ok, I suggest putting right after the opening body tag.
Ben
Hi Ben,
Thank you for your reply.
Yeah, I would like to put the second GTM code after the opening body tag, but I cannot find it in the theme files?
Here is the functions.php file from my child theme, that was generated with Child Theme Configurator:
<?php
// Exit if accessed directly
if ( !defined( ‘ABSPATH’ ) ) exit;
// BEGIN ENQUEUE PARENT ACTION
// AUTO GENERATED – Do not modify or remove comment markers above or below:
if ( !function_exists( ‘chld_thm_cfg_locale_css’ ) ):
function chld_thm_cfg_locale_css( $uri ){
if ( empty( $uri ) && is_rtl() && file_exists( get_template_directory() . ‘/rtl.css’ ) )
$uri = get_template_directory_uri() . ‘/rtl.css’;
return $uri;
}
endif;
add_filter( ‘locale_stylesheet_uri’, ‘chld_thm_cfg_locale_css’ );
// END ENQUEUE PARENT ACTION
What do you think about the functions.php file?
If it is not suitable I could maybe try to generate one from WP codex?
Thank you for your responsiveness
Best regards,
Alex
You don’t want to add to template files, that is not the best practice. You would add to your functions file for best practice.
The child theme is fine although you really don’t need any of that, it’s not doing anything for you.
Your child theme function file can simply be:
<?php
add_action('virtue_after_body', 'custom_add_google_tag_code');
function custom_add_google_tag_code() {
echo 'script goes here';
}
The point i’m trying to ask is what did you try, the section for “script goes here” what did you add that can help me see why you got an error.
Ben
Hi Ben and thank you for your reply,
As I am not quite literate in codes, I entered your function above in my generated functions.php child theme file through the CMS before the “if ( !function_exists( ‘chld_thm_cfg_locale_css’ ) )”. It then gave me “missing_content” and after refreshing the browser your function disappeared from my generated functions.php file I pointed above.
I guess you meant that I can only make a functions.php file with your function above to load the second GTM code immediately after the opening body tag? OK, I entered the first GTM code in the head part of the “header-and-footer-scripts-inserter plugin”. Did you mean that I have to enter the second GTM code after the ‘script goes here’; in your function in my child theme functions.php file, so that the second GTM code can be loaded after the opening body tag? I actually don’t know where to enter the second GTM code for the opening body tag?
Thank you for your time
Alex
Hey,
We seem to be missing each other, how about this you post here what code you need right after the opening body tag and I’ll post back exactly how your functions.php file should look, you can remove everything in your functions.php file and paste in what I post after I get your code.
Ben
Hi Ben,
OK that sounds nice, thank you.
Here is the 2nd GTM code I want to put right after the opening body tag:
<!– Google Tag Manager (noscript) –>
<noscript><iframe src=”https://www.googletagmanager.com/ns.html?id=”
height=”0″ width=”0″ style=”display:none;visibility:hidden”></iframe></noscript>
<!– End Google Tag Manager (noscript) –>
Thank you,
Alex
ok, your entire functions.php file should only consist of this:
<?php
add_action('virtue_after_body', 'custom_add_google_tag_code');
function custom_add_google_tag_code() {
echo '<!– Google Tag Manager (noscript) –>
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id="
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!– End Google Tag Manager (noscript) –>';
}
Just so you know the id= part of the url should have an id. something like GTM-XXXXXXX
Ben
Hi Ben,
Thank you very much.
Yeah, I removed the Id, so that it is not seen in the forum, even though it doesn’t matter. OK, basically I needed to put the entire code in the place of “script goes here”.
I got the idea now, I tried it and it works fine with GTM on the right places. 🙂
Thank you very much for your support. I am very happy.
Kind Regards,
Alex