How do WordPress Hooks Work?

Why were WordPress hook created? Why do we need them?

WordPress hooks solve a fundamental problem with dynamic function calls in PHP: How do you insert 3rd party functions with a static function call? But also, how do you use one single call to include an unknown number of functions?

What are functions and function calls?

Looking into this more, PHP has two segments of code that deal with functions: the definition and the call.  Here is an example of the two in the respective order:

<?php
function my_new_code($name) {
  echo "Hello " . $name;
}

my_new_code('World');
?>

In that example, I, as a single author, defined the name in the definition and the call.  That example was also a case of one definition and one call.

What are WordPress Hooks?

In order to include multiple functions with a single function call, which doesn’t know the names of the functions, we need to use an indirect method and this is where the “WordPress Hook” method comes into play. For this case were going to use a predefined call and definition to do the work for us. First were going to call the fetching function.

<?php
do_action('the_location', 'World');
?>

This function call, named do_action, is going to load a function in the same manner as the first example and send it the value “the_location”. The function definition, named do_action, will then call all the 3rd party functions that are in the group “the_location” and insert them into this location in the code.

How does the do_action function find out about 3rd party functions?

The do_action function finds out about the functions it should include by a sibling function named add_action, which creates a list of all functions. You can thing of add_action as the boss and do_action as the worker. This function, like its sibling, is a predefined function in WordPress, so you only need to call it. This function call is often included next to the function definition its mentioning. The following is an example of defining my_new_code and asks WordPress to hook it on:

<?php

function my_new_code($name) {

echo “Hello ” . $name;

}

add_action(‘the_location’, ‘my_new_code’);

?>

To rap it up, you define a function, add it to the list with a given location, then when the location gets called, your function is included. Here is another nice point about WordPess Hooks, the do_action could be written by someone else, such as a theme developer or WordPress its self (Have you heard of wp_head & wp_footer anyone?). The following is the code put all together so you can clearly see what is required:

<?php
function my_new_code($name) {
  echo "Hello " . $name;
}
add_action('the_location', 'my_new_code');
do_action('the_location', 'World');
?>

Both examples out put the same thing: “Hello World”, but here is an example of how the second one could be expanded:

<?php
function my_hello_code($name) { echo "Hello " . $name; }
add_action('the_location', 'my_hello_code');
function my_what_is_up_code($name) { echo " What's up " . $name; }
add_action('the_location', 'my_what_is_up_code');
function my_bye_code($name) { echo " Bye " . $name; }
add_action('the_location', 'my_bye_code');
do_action('the_location', 'World');
?>

This will output: “Hello World What’s up World Bye World”.

This was about action hooks, what about filter hooks?

The difference between actions and filters, is that actions do not return anything, while filters will often return a variable that was passed to them, e.g. return $value;. Filter are also less likely to have print and echo within the functions they include.

Did you know that add_action calls add_filter? Which means they use the same code and create a single list of functions to include. Check it out for your self in Trac.

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

This entry was posted in WordPress. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

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>