How to hook into the footer

Posted by Andrea under Plugins

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. ;)

7 Responses to “How to hook into the footer”

  1. Dr. Mike Wendell Says:

    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. IndieLab Says:

    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. WPGlamour Says:

    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. Andrea Says:

    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. Terni Says:

    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. Bookmarks about Php Says:

    [...] - 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 [...]

  7. sandg Says:

    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

Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-Spam Image



google.load("language", "1"); var curstate = 0; var hasloaded = 0; function bnc_show_translated() { if (hasloaded == 0) { bnc_lang_callback(); hasloaded = 1; } for (i = 0; i < 7; i++) { var elem = $("bnc_original_" + i); if (elem) { if (curstate) { elem.show(); } else { elem.hide(); } } } for (i = 0; i < 7; i++) { var elem = $("bnc_trans_" + i); if (elem) { if (curstate) { elem.hide(); } else { elem.show(); } } } if (curstate) { $("bnc_trans_state1").show(); $("bnc_trans_state2").hide(); curstate = 0; } else { $("bnc_trans_state1").hide(); $("bnc_trans_state2").show(); curstate = 1; } } function bnc_detect_div(div_id) { var text = document.getElementById(div_id); if (text) { text = text.innerHTML; if (text.length > 0) { google.language.detect(text, function(result) { if (!result.error) { if (result.language != "en") { if (result.confidence > 0.25) { $("bnc_translating").show(); bnc_xlate_div(result.language, div_id, "en"); } } } } ); } } } function bnc_xlate_div(src_lang,div_id,o_lang) { var text = document.getElementById(div_id); if (text) { text = text.innerHTML; google.language.translate(text, src_lang, o_lang, function(result) { var translated = document.getElementById(div_id); if (result.translation) { translated.innerHTML = result.translation; } }); } } function bnc_lang_callback() { bnc_xlate_div("en", "bnc_lang_i_0_98", "en"); bnc_xlate_div("en", "bnc_lang_i_1_98", "en"); bnc_xlate_div("en", "bnc_lang_i_2_98", "en"); bnc_xlate_div("en", "bnc_lang_i_3_98", "en"); bnc_xlate_div("en", "bnc_lang_i_4_98", "en"); bnc_xlate_div("en", "bnc_lang_i_5_98", "en"); bnc_xlate_div("en", "bnc_lang_i_6_98", "en"); bnc_xlate_div("en", "bnc_lang_i_7_98", "en"); bnc_xlate_div("en", "bnc_lang_i_8_98", "en"); bnc_xlate_div("en", "bnc_lang_i_9_98", "en"); bnc_xlate_div("en", "bnc_lang_i_10_98", "en"); bnc_xlate_div("en", "bnc_lang_i_11_98", "en"); bnc_xlate_div("en", "bnc_lang_i_12_98", "en"); bnc_xlate_div("en", "bnc_lang_i_13_98", "en"); bnc_xlate_div("en", "bnc_lang_i_14_98", "en"); bnc_xlate_div("en", "bnc_lang_i_15_98", "en"); bnc_xlate_div("en", "bnc_lang_i_16_98", "en"); bnc_xlate_div("en", "bnc_lang_i_17_98", "en"); bnc_xlate_div("en", "bnc_lang_i_18_98", "en"); bnc_xlate_div("en", "bnc_lang_i_19_98", "en"); } function bnc_startup() { bnc_xlate_div("en", "bnc_translate_info", "en"); bnc_xlate_div("en", "bnc_translate_info2", "en"); bnc_detect_div("bnc_lang_i_0_98");bnc_detect_div("bnc_lang_i_1_98");bnc_detect_div("bnc_lang_i_2_98");bnc_detect_div("bnc_lang_i_3_98");bnc_detect_div("bnc_lang_i_4_98");bnc_detect_div("bnc_lang_i_5_98");bnc_detect_div("bnc_lang_i_6_98");bnc_detect_div("bnc_lang_i_7_98");bnc_detect_div("bnc_lang_i_8_98");bnc_detect_div("bnc_lang_i_9_98");bnc_detect_div("bnc_lang_i_10_98");bnc_detect_div("bnc_lang_i_11_98");bnc_detect_div("bnc_lang_i_12_98");bnc_detect_div("bnc_lang_i_13_98");bnc_detect_div("bnc_lang_i_14_98");bnc_detect_div("bnc_lang_i_15_98");bnc_detect_div("bnc_lang_i_16_98");bnc_detect_div("bnc_lang_i_17_98");bnc_detect_div("bnc_lang_i_18_98");bnc_detect_div("bnc_lang_i_19_98"); } google.setOnLoadCallback(bnc_startup);
Close
E-mail It
This blog has been fine-tuned with 13 WordPress Tweaks.