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 wrote a plugin which uses wp_enqueue_script() which will include the snippet in either the header or footer depending on the parameters you pass it. The in_footer parameter works great - if the theme utilizes the wp_footer() hook.

What are my options if I want to include the JS snippet directly before the </body>tag if a theme is not using wp_footer() hook?

share|improve this question
    
I'm guessing you're thinking of this as a fallback for themes that aren't using wp_footer()? You could append it to the body from within wp_head(), but this is going to be ugly. There aren't really a whole lot of solutions available to you though. –  supajb May 18 '11 at 7:10
add comment

2 Answers 2

If the theme does not implement wp_footer() a lot of functionality that depends on it will break. For this reason, implementing wp_footer() is required to get your theme accepted in the WP.org directory. So it is not unreasonable to break when wp_footer() is missing.

Be sure to add a notice in your FAQ that you require wp_footer() in the theme. You can also add a check in your plugin admin page: checking for the existence of wp_footer() can be as simple as loading a page with a special $_GET variable in the background, which will trigger a hook that outputs some control code in wp_footer, and reading the result to see whether this control code can be found.

share|improve this answer
    
Thanks for the reply. I wasn't sure how to text for existence of wp_footer() but that makes perfect sense. I'll add that into my plugin. –  Casey May 18 '11 at 15:37
    
Thanks for that information, I wasn't sure how to check for existence of that hook but that makes perfect sense. –  Casey May 18 '11 at 15:38
add comment

You could use output buffering and append the script to the end. But you shouldn’t.

  1. It is slow.
  2. It is not safe. Some other plugin could run right before you do and compress everything to gzip. You would just invalidate the whole page in this case.
  3. The user may have removed wp_footer intentionally.

Aside: </body>, the closing tag, is optional. I haven’t used it since 2001. So that’s nothing you can rely on.

share|improve this answer
    
Hah I actually considered of using output buffering but I'll steer away from that! Thanks –  Casey May 18 '11 at 15:39
add comment

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.