1

I've got a child theme activated and I don't see any head and body in my child theme to place the GTM snippet.

How do I do this? I rather not paste in the whole head and body in the child theme to overwrite the parent theme and miss out on updates.

0

If your theme is any good it will have the wp_head hook in header.php. In your child theme's functions.php you can add an action to include anything you want. Like this:

add_action ('wp_head','wpse359909_add_gtm');
function wpse359909_add_gtm () {
  echo 'whatever code you want to add';
  }
3
  • (or better yet, put this in a custom plugin, so the child theme can still get updates) – WebElaine Mar 3 '20 at 14:00
  • I just wanted to avoid extra plugins as well :) And what about the code for body? Since Google tag manager should be place in the head and top of the body. – Mikkel Damsgaard Mar 4 '20 at 11:30
  • You'll have to choose. It's either within the <head> tags or at the beginning inside the <body> tag. You can't have both. The latter has a different hook, wp_body_open but not all themes implement this. – cjbj Mar 4 '20 at 12:29
0

I use the Code Snippets plugin to make managing injected code a little easier

enter image description here

enter image description here

add_action( 'wp_head', function () { ?>


<!-- head script here -->


<?php } );

add_action( 'wp_body_open', function () { ?>


<!-- body script here -->


<?php } );

Note: This uses the somewhat newer wp_body_open hook introduced in WordPress 5.2 (May 2019), make sure your WordPress is 5.2+ and that your theme isn't too old.

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.