I'm having problems finding the right hook to use for my plugin. I'm trying to add a message to the top of each page by having my plugin add a function. What's the best hook to use? I want to insert content right after the <body>
tag.
EDIT: I know it's three years later now, but here is a Trac ticket for anyone who is interested: http://core.trac.wordpress.org/ticket/12563
EDIT: July 31st, 2019
The linked Trac Ticket was closed as this feature was added in WordPress 5.2. You will find the Developer notes for this feature here (requires JavaScript enabled to display):
Miscellaneous Developer Updates in 5.2
I will not update the "correct answer" to one that mentions 5.2 for historical reasons, but rest assured that I'm aware and that the built-in hook is the correct one to use.
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: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.Hope that helps. Sorry if I misunderstood.
Changes, changes, changes. So it appears that since March 2019 (from WP 5.2) we have a little nicer way to do this.
There is a new function
wp_body_open()
. To support it, your theme has to call this function right after<body>
opening tag:And then you can use it in the same way you use
wp_head
orwp_footer
hooks to print anything just after<body>
.A very, very, very dirty solution would be: