Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I would like to inject some elements directly after the <body> tag.

Is that possible using only WordPress hooks?

share|improve this question
1  
Please, Nippysaurus, do it without hacking the heck of what not need to be hacked... @kaiser's the correct answer :) –  brasofilo Nov 21 '12 at 3:45
1  
I'll have to take your word on this ... I ended up going with a child template so didn't end up implementing any of these solutions :) Very new to WordPress development (this is my second site) so its interesting to see how to solve these sort of solutions. –  Nippysaurus Nov 21 '12 at 4:45

3 Answers 3

up vote 4 down vote accepted

Just add a custom hook to your template:

<body>
<?php do_action( 'wpse73370_custom_hook' ); ?>
share|improve this answer

You can create a plugin that is based on JavaScript and uses prependTo in order to place a tag right after the starting body tag. Check this out for a reference.

share|improve this answer
    
And if the site visitor disabled javascript? –  Ralf912 Nov 20 '12 at 23:38
4  
This is the hackiest hack ever :p –  Nippysaurus Nov 20 '12 at 23:53
1  
@Nippysaurus Indeed (+1), but output buffering is nothing better. –  kaiser Nov 21 '12 at 0:41
1  
@kaiser You think of nested output buffering. This is sometimes a bit tricky. Using output buffering with a callback, is not so tricky because the callback will be called on every ob_* function call and before the ob_* function call. –  Ralf912 Nov 21 '12 at 19:56

Yes, it's quite simple:

add_action( 'init', 'test_start_buffer', 0, 0 );

function test_start_buffer(){

    ob_start( 'test_get_buffer' );

}

function test_get_buffer( $buffer){

    return preg_replace( '#<body.+>#', '<body>', $buffer);

}

The first function will register a callback for the output buffering. The second function is the callback and will modify and return the HTML. Please read the PHP manual output bufferuing for more informations.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.