Add head and body function for google tag manager in child theme

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.

Topic child-theme wp-head Wordpress

Category Web


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.


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';
  }

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.