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'm working on a plugin that will add a top header bar (similar to the admin bar) directly after the <body> tag to sites. Since this will be used on multiple themes, I can't rely on the various hooks that some themes / frameworks provide.

I know that I can use the prepend() function to include raw markup, but I can't seem to find a solid way to have include a .php file instead.

share|improve this question

2 Answers 2

You'd have to parse the php in the file before it could be appended, so you'd either have to fetch it via AJAX or use wp_localize_script instead to pass some data and build the markup via JavaScript. You might get unpredictable behavior from browsers, since markup outside the body tag violates W3C spec and is not valid. I don't really see what advantage any of this would have over just using wp_footer(), aside from supporting a handful of poorly coded themes.

share|improve this answer
    
+1 and as addition: Don't add it right after <body>. Take a look at where the admin bar sits. –  kaiser Sep 20 '11 at 16:59
    
Yeah, the admin bar buries itself at the bottom of the site, but still within the opening and closing body tags. I could use put it in wp_footer, but depending on how the theme is coded it could be contained within a div container, which is what I'm trying to avoid. –  Norcross Sep 20 '11 at 22:41
    
the other issue is that if I go the same route as the admin bar, then I have to worry about CSS placement that could interfere with the admin bar if it's being used. so that route won't work. –  Norcross Sep 21 '11 at 13:02
    
@Norcross - I don't see how placing it outside the body tag solves either of those, you have to position it via css regardless. also - create a static html page with markup outside the body tag and see how all webkit browsers move it inside the body automatically. you should just rethink this, you're going to get unpredictable behavior from different browsers. –  Milo Sep 21 '11 at 14:45
    
maybe I'm not explaining this well. What I want is to place code directly after the opening body tag (most likely before the header div), not outside the body tags themselves. –  Norcross Sep 21 '11 at 20:31

If the theme follows WP's theme coding "standards", wp_footer should be located just before </body>. But many themes don't, some call this hook within a #footer div or whatever. I'm actually doing this myself with a few of my themes, because I'm forced to accommodate stupid plugins that insert text or other visual elements within wp_footer. Technically your's fits the description, but I'm talking about the ones that display credits, copyright messages and that kind of things :)

Even so, this shouldn't be a problem for your plugin, unless the div containing the wp_footer output has a relative position. In such cases you could:

  • include the .php file normally in your wp_footer action
  • hide your markup by default (set a display:none on it)
  • use jQuery to find the container of your HTML, and prepend it to the </body>, then show it, like:

$('#my-plugin-bar').prependTo('body').show();

And set top:18px if $('#wp-admin-bar).length > 0

There's no need to fire ajax requests to do this, or build the markup with jQuery...

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.