سؤال

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>
هل كانت مفيدة؟

المحلول

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/

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى wordpress.stackexchange
scroll top