wp_body_openaction-hookWP 5.2.0

Allows developers to inject code immediately after the opening <body> tag.

The hook fires when wp_body_open() is called. The function is used in a theme immediately after the opening <body> tag.

When adding HTML through this hook, keep in mind that it may break the template layout or not fit the theme design.

This hook is often used to output invisible HTML elements such as <script> tags or metadata.

Since WordPress 5.2, themes should call this function. To do this, call the function immediately after the opening <body> tag in the theme, for example:

<body <?php body_class(); ?>>
<?php wp_body_open ();?>

Usage

add_action( 'wp_body_open', 'wp_kama_body_open_action' );

/**
 * Function for `wp_body_open` action-hook.
 * 
 * @return void
 */
function wp_kama_body_open_action(){
	// action...
}

Examples

#1 Output any content

Suppose the theme's header.php file contains:

...
<body>
	<?php wp_body_open(); ?>
...

Output something. Add the code to functions.php or implement it as a plugin:

add_action( 'wp_body_open', 'any_html_css_js' );
function any_html_css_js() {
	?>
	Output any HTML, CSS, or JavaScript (see the warning above)
	<?php
}

#2 More examples

See wp_body_open().

Changelog

Since 5.2.0 Introduced.

Where the hook is called

wp_body_open()
wp_body_open
wp-includes/general-template.php 3445
do_action( 'wp_body_open' );

Where the hook is used in WordPress

wp-includes/default-filters.php 722
add_action( 'wp_body_open', 'wp_admin_bar_render', 0 );