Using Action Hooks in WordPress Child Themes

In this post we’ll review how to write a PHP function and go over the basic idea of how you can use Action Hooks in your WordPress Theme. We’ll take a look at a practical example of injecting a Welcome Blurb into your Theme without touching the existing code and we’ll also look at how to remove existing content being injected into Theme Hooks.

Packing Up A Function

Action hooks are in a lot of WordPress Themes nowadays. There’s a good reason for that but you’re probably wondering what the big deal is right? They’re such a big deal because firstly, they’re incredibly easy to use and secondly, because they’re extremely powerful.

If you want to get started with them we’re going to have to take a look at how to write a PHP function again. Don’t worry, we’ll keep it pretty simple.

  1.  
  2. function pretty_basic() {
  3.  
  4.    // This is a PHP comment.
  5.  
  6.    // PHP "stuff" goes here.
  7.  
  8. }
  9.  
  10. function still_pretty_basic() { ?>
  11.  
  12.    <!– HTML comment now –>
  13.  
  14.    <!– Notice how I "broke out" of writing PHP? –>
  15.  
  16.    <!– I added closing and opening PHP tags just inside the curly braces –>
  17.  
  18.  
  19.  
  20.    <?php // I can even write more familiar-looking PHP here ?>
  21.  
  22. <?php }

So that’s how you write a PHP function. It’s pretty easy. It’s a little package of “stuff” like more PHP or HTML that you can write in 1 place—like your functions.php file—and call in another place like so:

  1.  
  2.    <?php pretty_basic() ?>
  3.  
  4.    <?php still_pretty_basic() ?>

You’ve seen the same thing before with WordPress functions like wp_list_pages() or the_content(). These functions are packed with “stuff” deep in the bowels of the WordPress core and output wherever they appear in your template files.

The Basic Idea Behind Action Hooks

A lot of really smart theme developers have started adding what are essentially empty functions to their themes ready to be filled up with stuff. We call these Action Hooks. I first noticed them in the Tarski theme and Tarski theme developer Benedict Eastaugh does a really good job of explaining why you’d want to use them and how to add them to your theme. But you’ve probably already seen them before.

WordPress Themes use a pair of default hooks called wp_head() and wp_footer(). If you know what those are you know what we’re doing here. Those two function calls are for adding “stuff” to your WordPress theme without editing the template.

In a nutshell here’s what Action Hooks will mean to you if your favorite theme uses them: you can add any content you like—extra WordPress functions, plugin calls, singing and dancing jQuery-powered sliders—simply by adding your function to an existing action hook. You won’t have to edit the original template files. And your additions will be safe from any upgrades the theme author makes.

Adding Content To An Action Hook

Let’s do something practical we’ll add a “Welcome To My Blog” blurb just below the header of the Thematic Theme using the Thematic Action Hook, thematic_belowheader(). Place the following code snippet in your Child Theme functions.php file.

  1.  
  2. // First we make our function
  3. function childtheme_welcome_blurb() {
  4.  
  5. // We'll show it only on the HOME page IF it's NOT paged
  6. // (so, not page 2,3,etc.)
  7. if (is_home() & !is_paged()) { ?>
  8.  
  9. <!– our welcome blurb starts here –>
  10. <div id="welcome-blurb">
  11. <p>Welcome to <?php bloginfo('name'); ?>.</p>
  12. </div>
  13. <!– our welcome blurb ends here –>
  14. <?php }
  15.  
  16. } // end of our new function childtheme_welcome_blurb
  17.  
  18. // Now we add our new function to our Thematic Action Hook
  19. add_action('thematic_belowheader','childtheme_welcome_blurb');

Make sure you read the comments in the code snippet. It’s easy as cake! Want to add something to a WordPress Theme without touching the files and making a headache for yourself when it comes time to upgrade? Just find out where the Action is!

Removing Existing Content From Actions

In some themes, like Thematic, some of the Action Hooks are already partly filled up with stuff. This might look a little unusual if you’ve never seen it before but it’s insanely powerful. You can basically unplug parts of your theme.

Here’s the basic syntax for removing content from your Theme that’s already being hooked-in.

  1. // Unhook default Thematic functions
  2. function unhook_thematic_functions() {
  3.     // Don't forget the position number if the original function has one
  4.     remove_action('thematic_hook_name','thematic_function_name',postitionnumber);
  5. }
  6. add_action('init','unhook_thematic_functions');

And as an example, here’s a code snippet that will remove the entire main navigation menu from your Thematic Theme. Just pop it into your Child Theme functions.php and let it rip.

  1. // Unhook default Thematic functions
  2. function unhook_thematic_functions() {
  3.     // Don't forget the position number if the original function has one
  4.     remove_action('thematic_header','thematic_access',9);
  5. }
  6. add_action('init','unhook_thematic_functions');

Combine these 2 concepts with a hooked-up WordPress Theme and you can do practically anything.

How To Modify WordPress Themes The Smart Way

This post is part of the series How To Modify WordPress Themes The Smart Way.

  1. WordPress Child Theme Basics
  2. Modular CSS in WordPress Child Themes
  3. Using Filter Hooks in WordPress Child Themes
  4. Using Action Hooks in WordPress Child Themes

Don't forget: You should follow me on twitter here.

Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

7 Comments

  1. Posted May 25, 2009 at 8:58 am | Permalink

    Hi Ian,

    great post , but i m a bit confuse on this post and the filter post , is there any good compare when you should use filter and when using action is better ?

    • Posted May 25, 2009 at 9:03 am | Permalink

      It depends on what the Theme author has done. Sometimes they’ll have made something editable with Actions, sometimes with Filters. The choice has been made for you and you shouldn’t have to decide.

  2. Posted May 25, 2009 at 3:52 pm | Permalink

    Ian, I’ve learned so much from you!
    You have a talent for breaking things down in a manner that makes sense to me.
    Thanks for this.
    a

  3. gahlord
    Posted May 28, 2009 at 9:15 pm | Permalink

    thanks for your great framework and series.

    I used the code on this page and the output on the front-end is as-described. But the admin side is now throwing a lot of :

    Warning: Cannot modify header information – headers already sent by (output started at /wp-content/themes/thoughtfaucet/functions.php:2) in /wp-includes/functions.php on line 698

    Warning: Cannot modify header information – headers already sent by (output started at /wp-content/themes/thoughtfaucet/functions.php:2) in /wp-includes/functions.php on line 699

    I probably just copied something out wrong maybe?

  4. David Hedley
    Posted June 5, 2009 at 12:30 pm | Permalink

    Hi Ian,
    Excellent post, though I’m still trying to get my head round this!
    I want to insert 2 x fixed width divs inside #header of Thematic 0.9.5, moving all your header content into the left div and creating some custom content in the right div. Not sure whether I should be unhooking or creating a new action hook from scratch and somehow referencing some of your content in header-extensions.php?!?! Could you possibly offer any advice or instructions?
    Any help would be appreciated.
    David

  5. Trevor
    Posted August 6, 2009 at 1:52 pm | Permalink

    Thank you for writing this awesome post! It is really excellent writing, and it helped me understand how to take control of the Hybrid theme framework. I can’t wait to read the rest of the series!

    • Posted January 17, 2010 at 10:50 am | Permalink

      Hey Ian,

      I find it a nuisance to have to find the position number of every hook when I’m removing it from my actions. If you could please document each position beside the function in the template files (e.g. index.php) it would be helpful – rather than digging through helper and extension files when I’m remote editing.

      Cheers
      Jacob

19 Trackbacks

  1. By links for 2009-05-25 | Links | WereWP on May 25, 2009 at 9:04 am

    [...] Using Action Hooks in WordPress Child Themes (tags: WordPress tutorial hooks) Leave a Reply Click here to cancel reply. [...]

  2. [...] Here is the original: Using Action Hooks in WordPress Child Themes [...]

  3. [...] more here: Using Action Hooks in WordPress Child Themes Categories: wordpress Tags: action – action-hooks – adsense-ads – beta-telah – causing-some – [...]

  4. [...] Using Action Hooks in WordPress Child Themes – This post by Ian Stewart at ThemeShaper goes over how to use action hooks in your WordPress theme. Also includes a practical example of how to insert a “welcome blurb” on the homepage of your blog, without touching any existing code. [...]

  5. [...] Ian Stewart explains action hooks for Child Themes [...]

  6. [...] Action Hooks For Child Themes by Ian Stewart [...]

  7. [...] Using Action Hooks in WordPress Child Themes [...]

  8. [...] Using Action Hooks in WordPress Child Themes – This post by Ian Stewart at ThemeShaper goes over how to use action hooks in your WordPress theme. Also includes a practical example of how to insert a “welcome blurb” on the homepage of your blog, without touching any existing code. [...]

  9. By Dan Cole » How do WordPress Hooks Work? on June 20, 2009 at 2:20 pm

    [...] post by other authors: An Introduction to WordPress Action Hooks, Using Action Hooks in WordPress Child-Themes, Using Filter Hooks in WordPess Child-Themes. [...]

  10. [...] 20. Using Action Hooks in WordPress Child Themes [...]

  11. [...] 20. Using Action Hooks in WordPress Child Themes [...]

  12. By Structure du framework Thematic | Eiffair on August 27, 2009 at 2:41 pm

    [...] Utiliser les hooks d’action pour un thème enfant : Using Action Hooks in WordPress Child Theme [...]

  13. [...] Ações/Filtros: não só pra criar plugins, mas também pra modificar funções padrão ou criar os “child themes”. [...]

  14. [...] about Action Hooks By Mushon 20:18, Nov 16th, 09 Uncategorized filters hooks php thematic tips wordpress Comments [...]

  15. [...] Using Action Hooks in Child Themes and Using Filter Hooks in Child [...]

  16. [...] Using Action Hooks in Child Themes and Using Filter Hooks in Child [...]

  17. [...] Using Action Hooks in Child Themes and Using Filter Hooks in Child [...]

  18. [...] Posted May 25, 2009 at 8:58 am | Permalink [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/web site in attribution. In addition, you may find yourself fitter, happier and more productive. Comment away.

Subscribe without commenting