Search
Close this search box.
Search
Close this search box.

WordPress Hooks

Action Hooks

Modifying WordPress core files is a big no-no, so whenever you want to change existing functionality or create new functionality, you will have to turn to hooks.

While designing your themes, often times you want to be able to push dynamic content to a particular area of it.  The do_action(), add_action() functions of WP are a great tool to use.

These functions in wordpress allow you to specify a point in the code to trigger or initiate another segment of code. Like an extension to the original code that’s placed there. This is used so that different plugins that are created can use those ‘hooks’ in the wordpress code to add to the original core code without having to actually edit the core code itself.

do_action()

do_action creates the hook for things to hang. do_action is the first domino in the chain of events with hooks. However, alone, it means nothing and does nothing. Simply, it tells WordPress to search to see if any functions are attached to it to fire. In other words, what the do_action() function does is set’s a label in a specific point in your code for your plugin or theme code to hook into with the add_action() function.

For example, I place a do_action() right above the blog area of my theme.

do_action('mytheme_aboveblog');

What you’re doing when you add the do_action is make it so that at that place in your code you can inject some other code into it. What makes this different then actually hardcoding the function name there? Using the do_action() allows multiple functions to access that particular point in the place you put it at, not just one, which you can use to set different events to occur to trigger them to display or be used there.

add_action()

add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1)

The add_action finds the label specified in the $tag and hooks into it saying to that bit of code to “execute this function in addition to what you’re already doing, oh and pass these arguments.”

In simple terms,  add_action() tells WordPress to do something when it arrives at the specified  do_action() hook.

remove_action()

This removes an action that was set with add_action(). Removing stuff works a bit differently:

function remove_stuff(){
    remove_action('hook_location','function_to_remove',$priority );
}
add_action('init','remove_stuff');

In english this sort of translates to: When WordPress runs the init hook, please remove the function called “function_to_remove” that is located on the hook called “hook_location” with a priority of $priority.

Init is just a WordPress action hook. In fact, it is the one of the earliest hooks that run when WP starts whirring.

To remove a hook, the $function_to_remove and $priority arguments must match when the hook was added. No warning will be given on removal failure.

You can find an excellent detailed article about remove_action here.

Filter Hooks

A filter hook is also a place in your plugin for other functions to tie into, but they work slightly differently than actions. Filters allow for data to be manipulated or modified before it is used.

The key difference between actions and filters is that actions are usually used for executing functions and filters are usually used for manipulating data.

apply_filters()

apply_filters($filter_name, $value_that_can_be_changed, $param_1, $param_2, ...)

This creates a hookable location for custom filters to tie into. The apply_filters() function can take an unlimited number of parameters but only the first two are required. The first is the name of the filter. The second is the variable that your function will receive, in other words,  the value we’re going to change.

add_filter()

add_filter($existing_filter_name, $custom_function, $priority = 10, $number_of_accepted_args = 1)

This attaches a custom filter to a hook created with apply_filters(). Make sure that the $number_of_accepted_args matches the number of the filter’s args or else WordPress will throw an error.

Learning Resources