Adding google tag manager

Hey Guys,

What could be the proper way to add the google tag manager script as close as to the head tag without using any plugins?

Hi @yokellocal,

I suggest that you use wp_head WordPress action for this. You will need to install a child theme and at the PHP code below to the functions.php file:

add_action('wp_head', 'head_information', 9999);

function head_information () { ?>
<!-- Your HTML Code Here -->
<?php }

Replace the <!-- Your HTML Code Here --> portion of the code to whatever your Google tag manager Code is.

Thank you.


Thank you!

How about adding one right after the body tag?

Hi There,

Please try with this code:

add_action( 'x_before_site_begin', 'print_script_before_site_begin' );
function print_script_before_site_begin(){
	<!-- Your script -->


There is an error in your suggested code, there is no “space” between head_information and ()
I got a constant error message until I removed it!
Thanks for the only article that have help me on the WWW!

Glad you were able to resolve it :slight_smile:

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