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.
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.
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';
}
I use the Code Snippets plugin to make managing injected code a little easier
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.