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.

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:

<?php pretty_basic() ?>
<?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.

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.

// Unhook default Thematic functions
function unhook_thematic_functions() {
// Don't forget the position number if the original function has one
remove_action('thematic_hook_name','thematic_function_name',postitionnumber);
}
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.

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

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

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

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.

Could you direct me to where I would find a reference of the available hooks for the TwentyTen theme? I want to stop specific parts of the theme from being called and I can’t find a reference for the exact names of the hooks available anywhere. One of the items I want to remove altogether is the Access (or Nav Menu) section of the theme, just like the below example you have for Thematic:

Thank Ian. I guess it’s back to Thematic for me then! I was hoping they would really try to do more with the 2010 theme, but aside from the header and background sections it seems just as limiting as Kubrick was. Maybe I’m missing something though. It’s been known to happen. 🙂

I’m configuring a child theme, but have run into an issue. I’m trying to remove the blog title and replace it with my own styled title. In my childtheme’s functions.php I have added:

function unhook_thematic_functions() {
// Don’t forget the position number if the original function has one
remove_action(‘thematic_header’,’thematic_blogtitle’,3);
remove_action(‘thematic_header’,’thematic_access’,9);
remove_action(‘thematic_header’,’thematic_blogdescription’,5);
}
add_action(‘init’,’unhook_thematic_functions’);

The thematic_access gets deleted as wanted but the blog title stays… I use the childtheme “thematicfeaturesite”. Any idea?

I am still new to this…
Questions: in your sample you list two items to remove: ‘thematic_header’,’thematic_access’, why are they both required? and what does the ‘position number’ refer to, where can I find a ‘position number’, is it the line number of one of those two items (I could not detect a ‘position number in the ‘thematic’ nor in the ‘twentyten’?