• I am creating a plugin that needs to add a snippet of HTML as the first child of the body element on every page/post. This needs to be done by the plugin, without requiring the site administrator to edit any theme files. I know I could simply use jQuery('body').prepend(), but I’d rather use a more WordPress-official way of doing this – if there is one. I’ve searched but can find nothing helpful. Am I missing something?

    Background:
    The plugin will add the Infusion UI Options component to any WordPress site. You can see this component in action here: http://build.fluidproject.org/infusion/demos/uiOptions/
    The HTML for this component should be as close to the first thing in the page as possible, to ensure that it happens early in the tab order.

    What I’ve tried:
    I’ve looked into Widgets, but as far as I can tell, they’re restricted to official sidebars, whose location is controlled by the theme. I tried creating a new sidebar for the widget, but in order to add it to the theme in the right place, either the side admin has to modify the theme files, or the plugin has to inject the code to add the sidebar, which is back to jQuery.prepend. A Widget also seems like a bit of overkill, since what I’m doing doesn’t require any configuration at all, and I don’t need any administrative interface to allow the site admin to add or remove the component as a Widget, since this is the only thing the plugin does, so add/remove would be accomplished by adding/removing (or disabling) the plugin itself.

    I’ve looked through the documentation on hooks and actions and I haven’t found anything that looks like it will help me do what I want to do.

    I’ve glanced at the code for a couple of plugins that allow you to add markup to the header area, and they seem remarkably long, which makes me suspect their approach would be overkill for me.

    So: Is there a “WordPress way” to do what I want to do, or should I just go ahead with jQuery.prepend?

    Thanks in advance for any advice.

    anastasia

Viewing 4 replies - 1 through 4 (of 4 total)
  • There is nothing standardized that will suit you. The wp_head action is way too early for you, and I guess wp_footer it too late.
    One solution would be to enable output buffering in wp_head and process the outcome in wp_footer, injecting your code right after the body opening element.

    But I think it’s overkill, and since the Infusion UI is already heavily Javascript dependent, I’d do it with jQuery.prepend() just like you suggested.

    Thread Starter acheetham

    (@acheetham)

    Anastis, thanks for the feedback (and nice name 🙂

    That’s the conclusion I was coming to, but I wondered if perhaps I was missing something. It’s nice to have some reassurance.


    anastasia

    One idea that you can try…

    Use the the_content filter to add your HTML code under any existing code. Something ike this…

    function add_my_content($content) {
        return $content.'<p>Add your HTML code here</p>';
    }
    
    add_filter ('the_content, 'add_my_content', PHP_INT_MAX);

    Indeed, however this filter might never execute depending on the page you’re viewing, e.g. in listing pages where only the_excerpt() is called.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Add HTML inside body tag on every page using plugin?’ is closed to new replies.