WordPress’ New Action Hook to Put Code Immediately After the “body” tag

Great news for WordPress developers – WordPress now has a theme action hook to put code immediately after the <body> tag! It’s called “wp_body_open“. The new tag was included in WordPress 5.2 in May 2019, and is slowly being adopted by theme developers (e.g. It was added to Beaver Builder Theme in April 2020 – screengrab below).

wordpress add code after body tag beaver builder theme
WordPress' New Action Hook to Put Code Immediately After the "body" tag 2

No more having to hack your theme to put Google Tag Manager or other scripts in the <body> section of your website! Just use an action hook in either functions.php or your functional plugin like so:

// Add code just after <body> tag
function custom_content_after_body_open_tag() {
    ?>
    <script>Custom script here</script
    <?php
}
add_action('wp_body_open', 'custom_content_after_body_open_tag');

Is this helpful?

Posted in

Toby Cryns

Toby Cryns is a freelance CTO, expert WordPress developer, and teacher.

He offers free advice to improve your freelance biz.

He also publishes small droppings every now and them to https://twitter.com/tobycryns and https://twitter.com/themightymo.com

Follow Toby's contributions on Github and WP.org.