How to hook into the footer

I think this comes up in the forum at least once a week, so here’s some handy tips & instructions. First though, some explanations of the terminology.

Hook – what a plugin does when we want it to “hook” into, or run with, whatever wordpress is doing.
Action – does what it says, it acts.
Function – in php, a function is a tiny bit of code that does something, especially if it’s something we want to do often. We write a function, then we call it.

Simply put, this is a two-step process. We add a function as an action, then we hook into WPMU . Kinda like catching a lift.

Are you ready to write a plugin? Hang on, because here we go:


<?php

/* We have to start our php with an opening statement.
If we really want to be fancy we can add some things here in the comments to tell everyone what this does. */


/*
Plugin Name: your snazzy new plugin
Description: Adds whatever we want to the footer
Author: your name
Version: 1.0
Author URI: where your plugin can be found, or your website
*/

function your_function_name () { ?>

/* see what we did there? We declared a function then closed our php again. Why? So we can add whatever html goodies we like. */

<p><em>This blog is provided by <a href="<?php get_settings('siteurl'); ?>"><?php get_settings('sitename'); ?></a>.</em></p>

/* Now every blog on our system will get this sentence inserted into the footer of whatever theme they used. And we did not have to edit every single theme. I know you picked something like 250 right? */


<?php }


/* We opened up our php again. we have to tell WPMU something important. */
add_action('wp_footer', your_function_name');


/* Ta da! This is the magic line folks. Let's close up shop now. */
?>

Make sure there’s nothing behind that last >, no whitespace, no line break. .Now, before you go too far, there’s one HUGE CAVEAT for this working. Every theme must have  <?php wp_footer(); ?> somewhere in the footer.php file. Preferably right where you want the contents of your new shiny plugin to show up. Most themes have it, so if you try this and it doesn’t show, check the theme’s footer file first.

I bet you’re already thinking of other things you could put here, like your Google analytics code. ;)

About andrea

Comments

  1. I can never remember if it’s wp_head or wp_header or wp_foot or wp_footer. I have to work that out some day. :)

  2. Great post, very helpful! The possibilities are truly endless for what can be hooked into the footer.

    I have one question about your post though…what 250 are you referring to in this sentence…”I know you picked something like 250 right?”

  3. But you have to check to see where exactly in the theme that function has been used and how will the template look once you add text or anything else to that function.

    @Dr. Mike Wendell,
    Just open footer.php and look for the function name and then use it in the code, you don’t have remember it by hart.

  4. Indielab – 250 means 250 themes. Imagine trying to edit them all manually. :)

    WPGlamour – yep, you’ll have to check and see how it looks too. For some things, like Google Analytics code, you can skip that part as it won’t be rendered on the page.

  5. Quick and neat as always. I would add a couple of lines.. like how/where to add the call to the footer function in themes that are not provided with and the same tutorial for the header.
    I mean the footer is ok but sometimes you would like to add it to the start of the page… like a navigation menu or google ads, and so on.
    I don’t know if there is such a thing in wordpress mu/wordpress themes but it would be nice to find a way to insert .js as well in the section.
    Let’s assume you want some nice js fx in this repeating section, you should be able to add js libraries in the head section.
    For example, a nice thing to add to all the blog network is something similar to what you can see here http://www.templatemonster.com where on mouse over the “network menu” comes down (scriptaculous is used here but u can use mootools or something else)

  6. This is great provided I want to track hits throughout the whole WPMU, rite? Any way to make google analytics work for individual blog? Tq

  7. Webmaster Code Promo says:

    Hello,

    Great intro, straight to the point. Thanks, I tink I now understand the “HOOK” feature.

    Now, a very basic question: where do I find the full list of HOOKs??? I’ve digged into wordpress/wordpressmu and google, without success. Is there such a list at all?

    Otherwise, how do you proceed to find the hook name (and feature attached to it) ?

    Thanks!

  8. Is it possible to hook into the footer of only certain blogs? i.e. if I want to hook into the footer of only my main blog or only the blogs that the site admin has created, is there a way I can do that?
    [rq=,,][/rq]

  9. Sort of. You would have to hook the footer on all blogs and test to see if the blog belonged to admin.
    [rq=100,0,blog][/rq]Geek of the week

  10. apologies if this seems a particularly stoopid question, but where do you place the script?!
    [rq=,,][/rq]

  11. [edit] fixed! sorry! ignore that last comment

    Thank you very much for this solution by the way!
    [rq=,,][/rq]

  12. For the really slow n00b, you omit an important usage instruction.

    Are we creating an actual plugin to be placed in the plugins folder?

    or

    Is this inserted into the existing footer.php file? (I’m guessing yes)

    It is an informative little tutorial though :)

  13. Yes, you would be creating a plugin and putting it in either the plugins or mu-plugins folder. In the mu-plugins folder, it would be applied across the site.

  14. Oh brilliant! Thanks for such a clear explanation – I was able to get Google Analytics into my work’s blogs with no worries! I’m now thinking of many other ways to use this!

  15. Minotauro says:

    This is what I see on the footer of all my blogs when I follow your instructions:
    http://img85.imageshack.us/img85/4152/test1263286428781.png

    Why is that happening?

    Thank you

  16. What a simple solution to a plaguing problem. Thanks!

    The way you have the plugin constructed, the PHP comments fall outside the PHP code, making them appear in HTML. Then again, because they are purely instructional, people would remove them (I think).

    • “people would remove them”

      Maybe not initially, but one would hope that seeing the extra strings in the footer would prompt that.

  17. Gracias :)

Trackbacks

  1. [...] – bookmarked by 4 members originally found by igaum on 2008-07-31 How to hook into the footer http://wpmututorials.com/plugins/how-to-hook-into-the-footer/ – bookmarked by 4 members originally [...]

  2. [...] WPMU Tutorials » How to hook into the footer [...]