Galen Gidman

Front-end and WordPress developer

An Introduction to WordPress Action Hooks

Note: If you want to read this post, feel free, but I highly recommend my more recent post on the subject over on the Treehouse blog.

If you’ve done any WordPress theme development in the past, you’ll probably find that you are already somewhat familiar with action hooks. A certain few are required for all themes, wp_head() and wp_footer() being the best-known. These are most often used to plug code required by plugins to the <head> or right below the <body>.

Actions hooks are also commonly used to add extra code into parent themes via the child theme. Let’s say a parent theme had an action hook called parenttheme_below_header(). If you were authoring a child theme, the easiest way to add code below the header would be to create a function containing the code you desire to put below the header, and then hook it into the parent theme via the action hook.

We’ll walk through this process more in-depth a little later, but first I’d like to go over how to create your own action hooks with the WordPress plugin API.

Creating action hooks

You’ll want to first decide where you will put your action hook. Going back to the prior example, let’s say we want an action hook below our header. Open functions.php in a parent theme and add the following code.

function parenttheme_below_header() {

Now, open up header.php and under the closing <header> tag, call parenttheme_below_header() like so.


At this point, we’ve got a working action hook. But we’re not doing anything with it yet! Let’s fix that.

Hooking actions to action hooks

Start by creating a new child theme. If you’ve never done this before, you can read how on the WordPress codex – don’t worry, it’s not hard.

Create a functions.php file in the child theme and add the following code.

function childtheme_hello_world() {
    echo '<h1>Hello World!</h1>';

add_action('parenttheme_below_header', 'childtheme_hello_world');

Save your code and view your live site. Assuming your child theme is activated and everything went correctly, there should be and <h1> reading “Hello World!” under your site header. If you see it, congratulations! You’re using action hooks.

Unhooking actions from action hooks

So far we’ve learned what action hooks are, how to create our own, and how to hook actions to them. Another thing you might find yourself wanting to do is unhook actions.

Let’s say in your parent theme there is an action hook that is being used to hook in a header image under the site title. We’ll call the action hook below the title parenttheme_below_title() and the header image action parenttheme_header_image().

You’re creating a child theme and you decide you don’t want a header image. The best way to get rid of it is simply to unhook the action.

remove_action('parenttheme_below_title', 'parenttheme_header_image');

Another example

As I mentioned at the beginning of the post, action hooks are often used to insert code required by plugins into the document. Let’s say your plugin required a certain stylesheet. You would put this in the <head> using the wp_head() action hook.

function sampleplugin_stylesheet() {
    $url = get_option('siteurl');
    $dir = $url . '/wp-content/plugins/sampleplugin/';
    echo '<link rel="stylesheet" href="' . $dir . 'essentialstylesheet.css">';

add_action('wp_head', 'sampleplugin_stylesheet');

Edit: the above is not the correct way to include stylesheets or scripts in themes and plugins. It was included by a far less-experienced me to serve as a proof-of-concept. This and this will teach you how to do it the right way.

Go forth and code

I hope this cleared up any questions you may have had about WordPress actions and action hooks. If you want to see more examples of them being used well, a good place to start would be Thematic by Ian Stewart.

Leave a Reply

Your email address will not be published. Required fields are marked *