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?
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityI 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?
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>
<?php
}
add_action('after_body_open_tag', 'custom_content_after_body_open_tag');
This will then render in your HTML as:
<body>
<div>My Custom Content</div>
Recommended reading:
https://developer.wordpress.org/reference/functions/do_action/
As commented by Junaid Bhura from WordPress 5.2 a new theme helper function wp_body_open
has been introduced that is intended for use as per the likes of other helper functions wp_head
and wp_footer
.
For example:
<html>
<head>
<?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
<?php wp_body_open(); ?>
<!-- BODY CONTENT HERE -->
<?php wp_footer(); ?>
</body>
</html>
In your theme functions.php file (or suitably elsewhere)
function custom_content_after_body_open_tag() {
?>
<div>My Custom Content</div>
<?php
}
add_action('wp_body_open', 'custom_content_after_body_open_tag');
You should ensure that the hook exists within the theme that you are wanting to inject-into as this may not be widely adopted by the community, yet.
If NOT, you will still need to follow the principle of extending the theme with a child theme with the exception that YOU would use:
<?php wp_body_open(); ?>
...instead of OR in addition to:
<?php do_action('after_body_open_tag'); ?>
Recommended reading:
https://developer.wordpress.org/reference/functions/wp_body_open/
template_include
or similar, however it is strongly advised against doing that because not only is it inefficient, it could be very unreliable if something changes in the parent due to an updatge. Extending the parent theme using a child theme is the best practice, it is predictable and expected, plus it gives you a great deal of control, such as using the example shown above. However if you want to use preg_replace
that's up to you...
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;
}
Add this code in functions.php
function my_function() {
echo'<div id="from_my_function"></div>';
}
add_action('wp_head', 'my_function');
<div>
in the <head>
. The browser will try to fix this by closing the head (inserting </head>
) just before the <div>
. This can break your page, because everything after the inserted <div>
will now be in the body not the head.
Feb 10, 2021 at 17:16