Web Development Handbook

Web Development Handbook

Development is fun in a funny way

What is WordPress Hook? Filter VS Action Hooks

What is WordPress Hook? Filter VS Action Hooks

0 votes
0 votes
0 comments
200 views
Share

By Nihar Ranjan Das, Published on June 12th 2023 | 6 mins, 536 words

Hooks are one of the core parts of WordPress. By hooks, we can easily interact from one piece of code to another piece of code at a specific, pre-defined spot. They control the communication between the core and plugins/themes.

There are two types of hooks in WordPress. Actions & Filters. To use any of those, you need to run a callback function and then need to register it with a WordPress hook for a specific filter or action.

Actions

These action hooks allow you to add data or perform a task. All actions will run at a specific point in the execution of the WordPress core, Plugins, and Themes. The callback function can perform so many tasks like,

  • Showing data to the user interface
  • Update database data
  • Send notifications (email, sms, alert)

The callback function for an action hook doesn't return anything to the calling action hook.

A basic structure of an action hook

function my_callback() {
    // do something
}
add_action( 'init', 'my_callback' );

Using add_action we add an action hook. The add_action function requires minimum two parameters. 

  1. string $hook_name which is the name of the action you want to receive after calling
  2. callable $callback is the name of your callback function.

There are also two additional parameters:

  1. int $priority which is the priority of the callback function
  2. int $accepted_args which is the number of arguments that will be passed to the callback function.

For example:

add_action('save_post', 'my_callback', 10, 2);

To register an action hook, there is a function called do_action.

do_action('save_post');


Filters

These filter hooks give you the ability to customize the data of callback during the execution of the WordPress Core, Plugin, and Theme.  The callback accepts a parameter, modifies it, and returns it. The callback function for a filter hook must need to return the data after modification.


By default, WordPress provides so many hooks but the cool thing is anyone can create new hooks and other developers can use those from their plugins, and themes. Filter hooks expect to have something returned back to the calling Filter hooks

We already see there are a few differences between action and filter hooks. Let's read the core differences between action and filter hooks.

A basic structure of a filter hook

function my_title( $title ) {
	return 'The ' . $title . ' was filtered';
}
add_filter( 'the_title', 'my_title' );

Using add_fiter we add a filter hook. The add_filter function requires minimum two parameters. 

  1. string $hook_name which is the name of the action you want to receive after calling
  2. callable $callback is the name of your callback function.

There are also two additional parameters:

  1. int $priority which is the priority of the callback function
  2. int $accepted_args which is the number of arguments that will be passed to the callback function.

For example:

add_filter('save_post', 'my_title', 10, 1);

To register a filter hook, there is a function called apply_filters.

apply_filters('the_title', $title);


Actions Hook VS Filters Hook

The main difference between action and filter hook is:

  • An action hook gets the data it receives, do something using the data, and returns nothing.
  • A filter hook gets the data it receives, modify the data, and returns the modified data.


If you like our tutorial, do make sure to support us by buy us a coffee ☕️

Comments

Default avatar

Are you interested to learn more?

Be notified on future content. Never spam.