wordpress - add text at top of the page with plugin

I'm creating a plugin.
In this pugin, I need to add some 'dynamic' text at the top of the page after 'body' or before 'get_header()'.

I have many themes. So I want to avoid adding a function in header.php such as '<?php  echo my_plugin_data(); ?>'.

thanks for your help.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You might need to be a little more precise in your description

get_header(); includes all of the page header <head></head> plus quite a bit of code after the opening <body>

I have included as a reference the header.php for thematic

With Thematic it is easy to do, as you have something to hook
<?php } thematic_before(); ?>

Most themes you don't have that, so you will end up having to filter the whole header, and using str_replace()

There is a proposal on trac to add a hook as a standard after the body as it is so widely used by various kinds of javascript, but I doubt that will be implemented in a rush.
Unfortunately there isn't a standard naming convention for "non standard" hooks in theme frameworks, thus testing for their existance might actually be slower than just using str_replace.()
Hooking straight into get_header should do what you need.


Just put the following in a plugin and activate it. The actions are processed before the header.php is output from what I can see.
function my_plugin_data()

add_action( 'get_header', 'my_plugin_data' );

Open in new window

@AndyBeard I thought the same as I include all of my initial page structure in header.php, but from what the OP was referencing, I thought they might have had all page constructors outside of the header.php file. I do agree though, there needs to be a post <body> open hook for use within header, and a pre </body> close hook in footer. I don't think I would use it that often, but it would make sense to have it just in-case.
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

For some reason my example code from thematic didn't attach, as it would have made my point a lot clearer.

For simple themes hooking get_header might work, but for theme frameworks it gets quite complex what might appear within the header.
<?php thematic_create_doctype(); echo " "; language_attributes(); echo ">\n";?>
<?php thematic_head_profile();?>


wp_head(); ?>


<?php if (apply_filters('thematic_show_bodyclass',TRUE)) { ?>
<body class="<?php thematic_body_class() ?>">
<?php }
thematic_before(); ?>

<div id="wrapper" class="hfeed">

<?php thematic_aboveheader(); ?>   

    <div id="header">
        <?php thematic_header() ?>
    </div><!-- #header-->
<?php thematic_belowheader(); ?>   

    <div id="main">

Open in new window

ronan1979Author Commented:
@AndyBeard, you said "Most themes you don't have that, so you will end up having to filter the whole header, and using str_replace()"

how to insert my text just after <body> with str_replace ?
ronan1979Author Commented:
@Kysol, your solution write a text a the top of the page but does not respect XHTML.

example of text in situation::

<div style="border:10px solid red;">This is text on top of the page without CSS tricks.</div>
So your header.php includes all the page constructors?

From what I've been reading, it might be impossible at the moment to add_filter() get_header... I tried and couldn't get it working. The only way I could seem to do it was to use PHP output buffers, but that might not be what you want.

The only way I could do so was a very messy way, and if you are wanting the appended header to be SEO friendly, this won't work. But if you don't care, it might be an avenue.

As I said, this is messy and personally I wouldn't do it this way unless I _really_ needed to. I'd just add stuff straight into the header.php
function js_header_append( $content )
      print $content .
            '<script>' . "\n" .
               'var e_body = document.getElementsByTagName( \'body\' )[ 0 ]; ' . "\n" .
               'var e_message = \'<div style="border:10px solid red;">This is text on top of the page without CSS tricks.</div>\'; ' . "\n" .
               'e_body.innerHTML = e_message + e_body.innerHTML; ' . "\n" .
            '</script>' . "\n";

   add_action( 'wp_footer', 'js_header_append' );

Open in new window

ronan1979Author Commented:
@Kysol, the text must be inserted in the raw HTML - it must work when javascript is disabled

right now i've added in the header.php <body><?php echo my_function(); ?>. but i want to avoid manipulating raw header.php
I try very hard not to get involved in actual programming, I just pay programmers to do it, and have to know enough to evaluate their methods.

Here is an EE article that covers regex on a body tag, though you only want to match on the opening body.

(note they are using preg_replace as body can contain other stuff)

function mod_body($headerstuff) {

$textyouwanttoinsert = "we want this after body";
return preg_replace('regexyouendupwith', '$textyouwanttoinsert', $headerstuff, 1);

I am not going to attempt to get the regex right but that should be enough to get you started.
I should add that not all themes use get_header so you will need to test if it exists

If it doesn't exist, it is still possible to filter the whole output buffer before it gets sent to the browser, but there are some significant performance issues in doing this.
ronan1979Author Commented:
@AndyBeard, i think you are right
but somehow the code makes no change

return preg_replace("/hello/", $textyouwanttoinsert, $headerstuff, 1);
ronan1979Author Commented:
the function my_filter_hello_world is not even called .. !!?
function my_filter_hello_world($text) {
  $text =  "hello 1";
  return "hello 2";

Open in new window

Like I said, I couldn't get add_filter to hook onto get_header. The only other option I can think of is to hook header/footer and use the buffering in PHP
function append_callback( $buffer )
   $append = '<div style="border:10px solid red;">This is text on top of the page without CSS tricks.</div>';

   return preg_replace( "#<body(.*?)>#is", "<body$1>$append", $buffer );
function append_buffer_start() {
    ob_start( "append_callback" );
function append_buffer_end() {

add_action( 'wp_head', 'append_buffer_start' );
add_action( 'wp_footer', 'buffer_end' );

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ronan1979Author Commented:
im new at wordpress.
if now properly working.

Thanks to all of you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.