Adding a after_body Action to WordPress

,

So there are times that you would like to be able to add code directly after the body tag of a site.  WordPress unfortunately does not have this capability built into it.  So by modifying the the header.php file of your child theme you can easily give you the ability to add a code directly in that spot.

<?php
  //add this directly after the body tag in all header files
  do_action('after_body');
?>

Then either in your plugin or within your functions file you can do the following.

<?php
//in this instance I am using it to output the noscript version of Google's Tag Manager
add_action('after_body','output_custom');
function output_custom() {
  ?>
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=XXX-XXXXXXX"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
<?php
}
??

 

Skills

Posted on

May 17, 2018

Submit a Comment

Your email address will not be published. Required fields are marked *