Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress hook directly after body tag

Tags:

php

wordpress

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.

like image 829
Brooke. Avatar asked Aug 27 '10 05:08

Brooke.


People also ask

How do I add code before body tag in WordPress?

You can use hook wp_footer to add the code. See the below example code. function prefix_footer_code() { // Add your code here. } add_action( 'wp_footer', 'prefix_footer_code' );

Where do I put my WordPress hook?

In the Constructor When most of us learn how to use OOP PHP in a WordPress context, we generally learn to put hooks in the class constructor. Class constructors are magic methods that run when the class is instantiated. This pattern leads to the hooks being added magically.


2 Answers

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.

like image 127
Jorge Israel Peña Avatar answered Sep 20 '22 17:09

Jorge Israel Peña


Creating custom hook is really easy in WordPress. in header.php (or anywhere you may need a hook) locate:

<body <?php body_class(); ?>> <div id="body-container"> 

and make it:

<body <?php body_class(); ?>> <?php body_begin(); ?> <div id="body-container"> 

That's our hook, now let's make it work. In functions.php add:

function body_begin() { do_action('body_begin'); } 

Now hook is ready to use, simply add any actions you need in functions.php:

function my_function() { /* php code goes here */ } add_action('body_begin', 'my_function'); 

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):

function my_function() { ?> <script> <!-- JavaScript goes here --!> </script> <?php } add_action('body_begin', 'my_function'); 
like image 22
Stan Avatar answered Sep 22 '22 17:09

Stan