WordPress hook directly after body tag

WordPress 5.2 or newer:

Use the wp_body_open hook.

WordPress 5.1 or older:

That's kinda difficult... Most themes don't have any hooks in that area. You could hook a javascript/html solution into wp_footer and display it at the top of the page... sort of how Stack Overflow does it, or how Twitter does their notifications.

This is the best reference for all the hooks included in WordPress: http://adambrown.info/p/wp_hooks/


Alternatively, if you are creating the theme yourself and/or can modify it, you can create an action yourself using WordPress' do_action function. This is also how they create their other hooks. So basically in your theme, you would go where you want to, right after the <body> tag, and do something like:

do_action('after_body');

You can also pass arguments to the action callback, see the linked documentation for information.

Then afterwards, you would simply use the add_action function to hook onto it.

add_action('after_body', 'my_callback');

Hope that helps. Sorry if I misunderstood.

Tags:

Php

Wordpress