admin管理员组文章数量:1418637
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?
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?
Share Improve this question asked Mar 7, 2016 at 8:13 RamananaRamanana 3051 gold badge2 silver badges6 bronze badges 1- For future readers developing their own theme, or with bad/old themes, see this docs note. – Walf Commented Jun 14, 2023 at 1:30
3 Answers
Reset to default 38Twenty 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/reference/functions/do_action/
UPDATE: JULY, 2019
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');
IMPORTANT
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/reference/functions/wp_body_open/
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');
本文标签: filtersInsert HTML just after ltbodygt tag
版权声明:本文标题:filters - Insert HTML just after <body> tag 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745293593a2651944.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论