• Hi newbie question:

    I want to add HTML right after the <body> tag similar to this function

    add_action('wp_head', 'my_function');

    is there a way? tx

Viewing 11 replies - 1 through 11 (of 11 total)
  • Open your theme’s header file and put the code you want after the body tag, if that’s not what you mean, you’ll need to clarify what it is you’re intending to do..

    Thread Starter prfbst

    (@prfbst)

    Well the the function I mentioned is usually used in a plugin or functions.php (im writing a plugin ) and i want to add some html (div “from my function”) to where it is now:

    <body>
       <div id="from_my_function"></div>
       <div id="wrap"></div>
    </body>

    There are variations like wp_head, wp_meta or wp_footer etc, but I was wondering if i can directly add it to the <body> tag.

    tx

    Thread Starter prfbst

    (@prfbst)

    sry im a bit unclear still so here is basically the function I have:

    <?
    function my_function() {
    ?>
         <div id="from_my_function"></div>
    <?
    }
    add_action('wp_head', 'my_function');
    ?>

    Ok so now instead of wp_head or wp_footer is there a way I can append it to the body?

    I don’t think there’s an action for that..

    You could do it with jQuery ..

    Or maybe write your own action after the body tag in the theme, and hook onto that… (assuming user defined actions or filters work in the header – not tried it)..

    I have the same problem. I need to insert a div just after the body tag for a plugin.

    If someone has the solution, please let us know (:

    Like i said, there’s no action or filter available to do that, unless you have control over the theme(so you could add one yourself), then the only option remaining i can see is to use JS to inject extra content after the body..

    Example assuming jQuery is loaded:

    jQuery(document).ready( function($) {
    	$('body').prepend('<h1>Hello world</h1>');
    } );

    Thanks, I think that will work ??

    You’ll be happy to know a trac ticket has been submitted asking for an action after the body tag, so should it gain sufficient traction you may see an action for what you ask in the future… ??

    https://core.trac.www.ads-software.com/ticket/12563

    Traction traction, please!!!

    There is a real need for this hook. If you search trough this forums will will find a lot of requests that, at the bottom line, all point to this need.

    Traction!, please! ??

    MAC

    DCc mikeschinkel@… added

    ??? ??

    Yes! That is what i am looking for!
    That would enable me to dynamicly load js variables at the right point.

    A hook right before </head> will do it also in my case because the content will be something like:

    <script type="text/javascropt>var_1 = <?php _e('Hello', 'mydom') ?>;var_2 = <?php _e('Goodbye', 'mydom') ?>;</script>

    while the vars are declared in a js file that is registered and enqueued.

    Or, is there a defined action to insert html AFTER the enqueued scripts and BEFORE </head>? This is needed otherwise the script itsself overwrites the values with defaults.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘add_action right after <body> tag’ is closed to new replies.