How Do WordPress Hooks, Actions and Filters Work? - WPQuickies

How Do WordPress Hooks, Actions and Filters Work? – WPQuickies

I often get asked how to change the way WordPress outputs some data or how to change the way WordPress core operates.

Aha – you need to learn about WordPress Hooks: Actions and filters!

What Are WordPress Hooks?

Hooks allow you to interact and modify WordPress core functionality and are used by plugins, themes and the WordPress core itself.

The official documentation about hook is at https://developer.wordpress.org/plugins/hooks/

How WordPress Works

WordPress is an event-driven PHP application.

So for a process such as “Update Post” there will be a series of events that run chronologically to complete the process. 

WordPress is an event-driven app

The events run WordPress core functions that complete the outcome of the event.

As a theme or plugin developer, you may want to run your function before, during or after the event to do something or alter how WordPress does something.

Some but not all of these functions have an associated hook that you can link to your callback function.  When the hook gets called, your custom function is run.

More than one function can be linked to a hook.

Types Of Hooks

There are two types of hooks you can use in WordPress development. Actions and Filters.

WordPress actions and filters, WordPress Hooks

Actions run a function when the hook is fired. The callback function runs inside the scope of the hook and has access to WordPress global variables and any available object data within the scope. 

Actions can change the way WordPress works at that point in the event. Actions don’t have to have any data passed to them.

Filters receive some parameter data, run a function when the hook is fired, and returns the parameter data to WordPress, allowing the developer to intercept and modify the data being passed back to WordPress.

Actions are usually reserved for notable changes to how WordPress works – adding options, popping up a dialogue box, creating custom post types, processing payments etc.

Filters are usually reserved for modifying data in transit to the following process or event.  Filters operate more subtly than actions.

Action Hooks In Core PHP Code

In the WordPress code, actions are hooked using the do_action function in this format:

do_action( 'action_name', [optional_arguments] );

e.g.

The do_action( ‘wp_head’ ); action can be hooked in to run custom code every time WordPress processes the site header.

Adding An Action Hook Callback Function

You can add the code in your plugin, the theme’s functions.php file or using a code snippets plugin.

Add your action callback function using the following format:

add_action( 'hook_name' , 'callback_function_name'  , $priority=10 , $num_arguments=1 );

e.g.

function noindex(){
  echo '<meta name = "robots" content = "noindex, nofollow">
';
}
add_action( 'wp_head', 'noindex' );

Filter Hooks In Core PHP Code

In the WordPress code, actions are hooked using the apply_filters function in this format:

apply_filters( 'filter_name', 'value_to_be_filtered' , [optional_arguments] );

e.g.

The apply_filters( ‘admin_footer_text’, string $text ); filter can be hooked in to modify the text displayed in the admin footer. “Thank you for creating with WordPress.”

Adding A Filter Hook Callback Function

In the WordPress code, actions are hooked using the apply_filters function in this format:

add_filter( 'hook_name' , 'callback_function_name'  , $priority=10 , $num_arguments=1 );

e.g.

function my_admin_footer_text( $footer_text ){
  return '<p>Powered by Zero Point Development</p>';
}
add_filter( 'admin_footer_text', 'my_admin_footer_text' );

Action and Filter References

Actions: https://codex.wordpress.org/Plugin_API/Action_Reference  

Filters: https://codex.wordpress.org/Plugin_API/Filter_Reference 

Summary

That’s my quick guide to WordPress hooks, actions and filters.

Do you still have questions about WordPress hooks, actions and filters?

Ask in the comments below.

#WPQuickies

Join me every Thursday at 1 pm Sydney time for some more WPQuickies – WordPress tips and tricks in thirty minutes or less.

Broadcasting live on YouTube and Facebook.

Suggest a #WPQuickies Topic

If you have a WordPress topic you want to see explained in 30 mins or under, fill out the form below.

https://forms.gle/mMWCNd3L2cyDFBA57

Watch Previous WPQuickies

Was this article helpful?
YesNo