0

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>

1 Answer 1

4

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/

2
  • Thanks. Do you know what action to hook CSS code?
    – Ooker
    Feb 20, 2021 at 14:49
  • If you are using wp_register_script() to register a handle for your stylesheets, the "WordPress way" to properly load the CSS is to enqueue them via a single wp_enqueue_scripts call like this: add_action( 'wp_enqueue_scripts', 'your_function_name' ); developer.wordpress.org/themes/basics/including-css-javascript/…
    – jdm2112
    Feb 20, 2021 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.