ThemeShaper Forums » Thematic


how to add code before </body>

(7 posts)
  • Started 11 years ago by cookieandkate
  • Latest reply from cookieandkate
  • This topic is resolved
  1. Hello, I need to insert a snippet of code right before the </body> tag on each page of my site. I know I've accomplished something similar with the head section, thanks to your help, but I'm no good at tinkering with PHP code. Does anyone know how to insert code immediately preceding the </body> tag?

    Thank you so much for your help!

    Posted 11 years ago #
  2. the same way you add anything to any hook.

    look at

    and you will see that thematic has a hook exactly where you are asking: thematic_after();

    Posted 11 years ago #
  3. proto

    Hello cookieandkate,

    I'd only add a little background for future reference to Helga's good advice! (Who by the way is an absolute angel of a helper!)

    You can do this by hooking onto one of Thematic's many hooks. Assuming you're new to this (apologies if you're not) Thematic comes with lots of hooks where you optionally add content / markup etc to. These hooks are like pieces of velcro and if you enter the right code you hook onto these velcro hooks markup and content.

    One way you can do this is to add your code to one of the velcro 'hooks' that are located before the end of the body tag. In thematic's case this is called "thematic_after;"

    So your code might look like this:

    // Action hook for placing code before end body tag
    function webmagic_code_beforeendbody() {
    add_action ('thematic_after', 'webmagic_code_beforeendbody');

    The "webmagic_code_beforeendbody" with a unique name, perhaps something like 'webmagic' (sub in your own name) to ensure it doesn't conflict with any of wordpress native commands.

    There are many other velcro type hooks in the footer, for example. To find out where the hooks are have a look in the Thematics "footer.php" and other files, you'll see them there.

    **You'd place the code snippet above in your child theme's functions.php file. Child themes are the way to go as your site can update but retain all your unique customisations.

    Posted 11 years ago #
  4. Thanks for our help! I finally understand how hooks work now. I tried using the code given by proto above, but I kept receiving error messages. I then based my code off the code that was working for my <head> section. Here it is:

    // CODENAME code goes before end body tag
    function CODENAME_code_beforeendbody() { ?>
        <?php }
    add_action ('thematic_after', 'CODENAME_code_beforeendbody');

    I assume that I did that correctly, considering that I didn't receive an error message this time. Hope the code helps anyone who may need to add code before the </body> tag.

    Posted 11 years ago #
  5. you got it... now just remember that add stuff to any hook works the same way and with great power comes great responsibility! :)

    Posted 11 years ago #
  6. proto

    Isn't Helga brilliant!

    Helga is somewhat of champion of helping people, she's be kind enough to help me out of many scrapes or tap me on the shoulder to see the bigger picture!

    Cookieandkate - thanks for posting your reply and solution here. I just thought I'd add one more thought. The code I gave was for your child theme "functions.php" file (assuming you're using a child theme!) If you're not, there are numerous reasons to use a child theme over modifying the Thematic theme directly, too many to mention here but it really does make your life SO much easier in the long run for updates etc. I tested the code I posted here before adding and just tested again and it seemed to work ok.

    Any errors may be because you might have been because the code I gave you was added somewhere else other than your child theme "functions.php" file or the PHP and HTML must be separated, otherwise it will cause errors! Just in case anyone was reading this thread and wanted to know :)

    Posted 11 years ago #
  7. Thanks again for your help. I was adding your code to my child theme's functions.php, so I don't know why it didn't work as provided.

    Posted 11 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.