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.
In this scenario, what i do is: Use Jquery to append or prepend things:
http://api.jquery.com/prepend/
I could not find any working example online, but I got one solution and I hope it might help anyone. It is quite simple, just add jquery and do whatever you want. The below example might help anyone.
Here is the website url https://wordpress.org/support/topic/add_action-right-after-ltbodygt-tag
With the help of the above answer, I have made a plugin which directly adds content after the body tag. Here is the sample code :
The most amazing thing is that I don't know how I have done the above code in an easy way, pulling the data dynamic right after the body tag. I hope my answer will help someone and give a better idea. Live working example : http://codecanyon.net/item/woocommerce-coupon-scratch-discount/9235287
WordPress has now addressed this by adding the 'wp_body' hook in version 5.2. You can now hook or inject into HTML body by doing:
Putting this in your functions.php file in your theme might be best for most basic users.
I searched the internet for answers to the same question but found nothing. I figured out away to to work around it. My plugin infinite Ad Pay is based on this method.
You need two hooks wp_head and wp_footer hook
Creating custom hook is really easy in WordPress. in
header.php
(or anywhere you may need a hook) locate:and make it:
That's our hook, now let's make it work. In
functions.php
add:Now hook is ready to use, simply add any actions you need in
functions.php
:or JavaScript (tracking code etc - this is not the perfect way though, it is a better practice to load JavaScript from
.js
files, but it is definitely better than adding JavaScript directly to template files):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/