Insert HTML just after <body> tag

I am using the WordPress theme Twenty Twelve (a child of it to be precise).

I want to know how to insert some HTML just after body opening, in just functions.php and not using header.php.

Is that possible?

Solutions Collecting From Web of "Insert HTML just after <body> tag"

Twenty Twelve does not have any hooks that fire immediately after the opening <body> tag.

Therefore you in your child theme which extends the parent Twenty Twelve theme, copy the header.php across to your child theme directory.

Open the header.php file in your child theme and just after the opening body tag add an action hook which you can then hook onto via your functions.php file.

For example in your twenty-twelve-child/header.php file:

<body <?php body_class(); ?>>

<?php do_action('after_body_open_tag'); ?>

Then in your twenty-twelve-child/functions.php file:

function custom_content_after_body_open_tag() {


    <div>My Custom Content</div>



add_action('after_body_open_tag', 'custom_content_after_body_open_tag');

This will then render in your HTML as:

<div>My Custom Content</div>

Recommended reading:

Add this code in functions.php

function my_function() {
 echo'<div id="from_my_function"></div>';

add_action('wp_head', 'my_function');

A very, very, very dirty solution would be:

/* Insert tracking code or other stuff directly after BODY opens */
add_filter('body_class', 'wps_add_tracking_body', PHP_INT_MAX); // make sure, that's the last filter in the queue
function wps_add_tracking_body($classes) {

  // close <body> tag, insert stuff, open some other tag with senseless variable      
  $classes[] = '"><script> /* do whatever */ </script><noscript></noscript novar="';

  return $classes;