In the question How to add code to Header.php in a child theme?, a recommended solution is to create a plugin, then hook into wp_head action by using this code:
add_action('wp_head', 'wpse_43672_wp_head');
function wpse_43672_wp_head(){
//Close PHP tags
?>
ADD YOUR PLAIN HTML CODE HERE
<?php //Open PHP tags
}
Looking at header.php, I see that <?php wp_head(); ?> belongs to <head>, not <body>. So which function to use if I want to hook into the body? I don’t see any <?php wp_body(); ?> in header.php, though I find there is <?php wp_footer(); ?> right before </body> in footer.php.
My goal is to add these code, in order to use the Off Canvas Sidebars plugin:
<body>
<?php do_action('website_before'); ?>
<!-- WEBSITE CONTENT -->
<?php do_action('website_after'); ?>
<?php wp_footer(); ?>
</body>
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The action you are looking for is wp_body_open(). I do not know if you are writing your own theme or not but if you are using a pre-built theme, the author of your theme may not have included this support.
If properly supported in your theme, any function hooked to this action will be called immediately after the page opening <body> element.
WordPress reference:
https://developer.wordpress.org/reference/functions/wp_body_open/
Additional theme reference:
https://generatewp.com/wordpress-5-2-action-that-every-theme-should-use/
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0