Some tracking and consent tools ask you to put part of their code right after the opening <body> tag in WordPress. The best-known example is the <noscript> part of Google Tag Manager. Scripts Organizer has Header and Footer locations, but no extra location for this one, because WordPress already has a hook for it.
This snippet prints your code with the wp_body_open action, so it lands directly after <body> without editing theme files. The example is the Google Tag Manager noscript tag; replace it with whatever your tool gives you.
The snippet
<?php
/**
* Print code right after the opening <body> tag on the front end.
* Replace the example between ?> and <?php with your own code.
*/
add_action( 'wp_body_open', 'dpc_body_open_code', 1 );
function dpc_body_open_code(): void {
?>
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXXXXX" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
<?php
}
Change GTM-XXXXXXX to your container ID, or replace the three lines between ?> and <?php with your own code.
Add it with Scripts Organizer
In Scripts Organizer go to Code Blocks → Add New and give the block a name. Set Trigger location to Everywhere, tick PHP under Script location, paste the code into the PHP editor and publish. Leave Action/Hook empty. The wp_body_open action only fires on front-end pages, so the code prints nothing in wp-admin.
Leave out a closing ?> at the end of the PHP editor. Anything after it, even an empty line, is sent to the browser before the page and can cause “headers already sent” warnings. The older version of this post ended with one.
How it works
wp_body_openis a WordPress action that themes call right after the<body>tag. Block themes get it from WordPress core, and classic themes callwp_body_open()in theirheader.php.- Priority
1runs the function before other callbacks on the same hook, such as the skip link, so your code is the first thing in the body. - Inside the function,
?>switches PHP off, so you can paste HTML and<script>tags as they are, without quotes or escaping.
Customise it
- Only for visitors, not for logged-in editors: add
if ( is_user_logged_in() ) { return; }as the first line of the function. - Only on some pages: return early with a conditional tag, for example
if ( ! is_front_page() ) { return; }. Conditional tags work here because the hook fires while the page is printed. - The other half of Google Tag Manager, the
<script>for the<head>, goes into a separate code block with Trigger location Conditions and Script location Header in HTML mode.
Gotchas
- A few older themes and page builders never call
wp_body_open(), so nothing is printed. Check the page source. With such a theme, add the call after<body>in a child theme’sheader.php, or use the theme’s own hook (Oxygen users see Oxygen Hook. Add code after body is opened). - Code printed here is not wrapped for you. Unlike the Scripts Organizer Header and Footer editors, a script needs its own
<script>tag. - If a cookie banner must approve tracking first, load the tag through the banner instead. See Free WordPress Cookie Consent with Scripts Organizer.
In the Scripts Organizer library Import it with one click from Scripts Organizer.