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.

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.

  • +1 and as addition: Don't add it right after <body>. Take a look at where the admin bar sits.
    – kaiser
    Sep 20, 2011 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, 2011 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, 2011 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, 2011 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, 2011 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:


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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