wp_body_open
Fires right after the opening <body> tag on front-end pages. Handy for skip links, notice bars and <noscript> fallbacks.
When it fires
wp_body_open runs when the theme calls wp_body_open() immediately after . Block themes always do. Classic themes are expected to since WordPress 5.2, but older or custom themes may not, and then your output won't appear.
It only fires on the front end. The has already been sent by now, so you can't add meta tags or head styles from here.
Parameters
None. Your callback takes no arguments.
Example: Show a notice bar at the top of the home page
add_action( 'wp_body_open', function () {
if ( ! is_front_page() ) {
return;
}
printf(
'<div class="site-notice" role="region" aria-label="%1$s"><p>%2$s</p></div>',
esc_attr__( 'Site notice', 'my-snippets' ),
esc_html__( 'Free delivery on all orders this week.', 'my-snippets' )
);
} );
Good to know
- If nothing shows up, search your theme's header.php for wp_body_open(). If it's missing, ask the theme author to add it.
- Style the bar from your stylesheet or with wp_add_inline_style() on wp_enqueue_scripts, because the is already finished here.