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

admin2025-01-07  3

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.

Share Improve this question edited Mar 3, 2020 at 13:45 cjbj 15k16 gold badges42 silver badges89 bronze badges asked Mar 3, 2020 at 13:35 Mikkel DamsgaardMikkel Damsgaard 112 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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';
  }

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736257081a410.html

最新回复(0)