Define Your Own WordPress Action Hooks

WordPress is based on the so-called Hook-System. This serves WordPress, and all extensions to involve functions in a certain place. The Hooks are not only to hook, but can also be used in custom extensions and thus create a better overview and offer additional interfaces for more possibilities to develop. Especially the second option is interesting when you create a theme, which aims to offer a variety of interfaces, or create a Plugin which can be expand by other authors or serve as a framework.

To use the possibility of the hooks, you can use the same function as WordPress (do_action()), and you don't have to create your own functions or methods.

In the following example function, which I put in the functions.php of the theme, of my Footer my_footer() will be a hook defined using the function do_action(). This can now be used and the hook is possible from the outside.

function my_footer() {
    do_action('my_footer');
}

I call the actual function in the template of the theme, in this case of course in the footer.php of the themes, in the example just before the closing body tag.

<?php my_footer(); ?>

</body>
</html>

Now I would like for a Plugin or any function in the theme to access this area. In addition, I write my own function, for example a function to put all the code snippets for statistical tools in one function. This new feature I call example_function(). To hook this function in a specific place, I can use the classic Hook-functionality of WordPress. With the help of add_action() I chose the Hook, which should be and hand over the function that I created. Following I provide a third parameter which guarantees the priority to hook, which is not compulsory. The default value for the priority is 10 and has no influence here, because I'm addressing only this one function via Hook.

add_action('my_footer', 'example_function', 1);

In order to understand the priority better, we create another function, which provides more content to the place of my Hook, so we hook into the function example_function_2() as well, but with a lower priority than the first function and thus it will hooked after the function example_function().

add_action('my_footer', 'example_function_2', 2);

With this option you can create a very clean solution and perhaps it can be useful for one or the other readers. Especially small code snippets that are stored in the footer, can be put in one function and on demand easier disabled. Everything can be controlled via Hooks and in the functions.php or on an option page of the theme in your backend.
This possibility is in fact in all WordPress versions available, including the newest WordPress version 2.8.

An additional great read is Nathan's "An Introduction to WordPress Action Hooks"

Comments are closed.

5 comments

  1. Justin Tadlock

    Great article on creating action hooks. Shouldn't the post be titled "Define your own WordPress action hooks" since you didn't cover filter hooks?

  2. Michael

    Thanks Justin. Yes, you are right with your headline. Nobody is perfect ;)

  3. inalgnu

    Great !! thank you !!

  4. Jauhari

    It's more easy to play for some WordPress Theme designer... Thanks for share

  5. Cyberia

    Interesting article, Thanks Michael.