How to differentiate between Action And Filter Hooks

Action and filter hooks in WordPress

Action and Filter Hooks in WordPress

We can describe the difference between action and filter hooks as:
Action hooks are used for a variety of tasks, including adding HTML, CSS, and JS files and running code. While the Filter hooks filter the data returned by a Database query or computed by any algorithm or returned by a function.

Action Hooks

These hooks are normally considered events, those are triggered when some specific action or event is performed. For example, if a post has been saved successfully ‘save_post’ and ‘save_post_{post_type}’ action hooks are triggered.

Function for action hooks

  • add_action()
  • do_action()
  • remove_action()
  • has_action()

Add action hooks:

It is usually used in plugins and themes to extend the default functionality of WordPress or any plugin of WordPress like WooCommerce. Themes also use hooks, so you can also extend the functionality of themes.

It can be used to add some content on the page at a particular place like “after product description”.

It can be used to determine where data is saved successfully in the database, now we can do our job. Like “order has been saved, now count total product purchased”. 

add_action('save_post_shop_order', 'my_callback_function');
function my_callback_function(){
	// do your job
Action Hooks WordPress

Trigger action hooks:

There can be two main reasons to trigger an action.

  1. Giving facility to third parties and other developers to extend your functionality. 
  2. Trigger the action so all activities related to a particular hook can be done like “send order emails to customer and shop manager”.

Remove action hooks:

The usage of remove action hooks can be explained as:

  1. Remove an action to prevent an infinite loop. For example, If we call a function inside the callback of an action hook that is triggered in that function. The relation between the “save_post” hook and the “wp_insert_post” or “wp_update_post” function is the best example of this scenario.
  2. Remove an action to prevent the conflict or remove functionality of WordPress, plugin, or theme.
remove_action('save_post_shop_order', 'my_callback_function');

Check whether an action has been added or not.

if( has_action('save_post_shop_order', 'my_callback_function') ) {
// do your job

Filter hooks

Filter hooks are typically used to give access to data. It can retrieve by a database, returned by a function, or calculate the results of any algorithm or mathematical problem. Filter hooks provide access to data and you can alter the data or apply more calculations to it.

woocommerce_get_product_price is a filter hook.

Function for Filter hooks

  • add_filter()
  • apply_filters()
  • remove_filter()
  • has_filter()

Add filter hooks:

Add filters and hooks in your plugin or theme to overwrite the default values returned by WordPress or WoooCommerce functions. It allows you to alter the default values.

add_filter('woocommerce_get_product_price', 'apply_discount', 100, 2);
function apply_discount($price, $product ){
	if( $price > 100 ) {
		$price = $price - ($price * 0.2);
	return $price;
Filter hooks WordPress

Apply filter hooks:

You can use this method to give access to use your plugin or theme data and alter it according to their needs.

// For better pracrtice and give access to third party you can use apply filters as given below.
add_filter('woocommerce_get_product_price', 'apply_discount', 100, 2);
function apply_discount($price, $product ){
	if( $price > 100 ) {
		$price = $price - ($price * 0.2);
		return apply_filters('woocommerce_get_product_discounted_price', $price, $product);
	return $price;

Remove filter hooks:

Removing a filter can be used to get the actual data produced by a function or method but It totally depends on the priority of hooks. in case you aren’t using the proper priority it can no longer work for you as it should.

remove_filter('woocommerce_get_product_price', 'apply_discount', 100, 2);

Check whether a filter has been added or not.

It has the same usage as the has_action method.

has_filter('woocommerce_get_product_price', 'apply_discount');

Important Note:

If you have any queries or facing any problem relevant to WordPress hooks or filters you ask your question in the comments sections.

3 thoughts on “How to differentiate between Action And Filter Hooks”

  1. Pingback: How to view all WordPress Hooks - WooCommerce Plugin Developer

Leave a Reply

Scroll to Top