wordpress - add text at top of the page with plugin

hi,
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.
LVL 4
ronan1979Asked:
Who is Participating?
 
KysolCommented:
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() {
    ob_end_flush();
}

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

Open in new window

0
 
AndyBeardCommented:
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.()
0
 
KysolCommented:
Hooking straight into get_header should do what you need.

http://adambrown.info/p/wp_hooks/hook/get_header

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

0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
KysolCommented:
@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.
0
 
AndyBeardCommented:
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();?>

<?php 

thematic_doctitle();
thematic_create_contenttype();
thematic_show_description();
thematic_show_robots();
thematic_canonical_url();
thematic_create_stylesheet();
thematic_show_rss();
thematic_show_commentsrss();
thematic_show_pingback();
thematic_show_commentreply();
wp_head(); ?>

</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

0
 
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 ?
0
 
ronan1979Author Commented:
@Kysol, your solution write a text a the top of the page but does not respect XHTML.

example of text in situation::

<body>
<div style="border:10px solid red;">This is text on top of the page without CSS tricks.</div>
0
 
KysolCommented:
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

0
 
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
0
 
AndyBeardCommented:
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.

http://www.experts-exchange.com/Programming/Languages/Regular_Expressions/Q_24462075.html
(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);
}
add_filter('get_header','mod_body');

I am not going to attempt to get the regex right but that should be enough to get you started.
0
 
AndyBeardCommented:
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.
0
 
ronan1979Author Commented:
@AndyBeard, i think you are right
but somehow the code makes no change

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

Open in new window

0
 
ronan1979Author Commented:
im new at wordpress.
if now properly working.

Thanks to all of you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.