has_action VS did_action in WordPress


Basically, We use the has_action() function to check if any action( callback ) has been registered for a hook.

WordPress has_action() function expects 2 parameters when it is called. The first parameter is $hook_name, and the second parameter $callback is a callback function.

Here first parameter $hook_name is required. This means you must need to pass the $hook_name with this function. The second parameter $callback is optional.

If the $callback parameter is not passed, it returns a boolean for whether the hook has anything registered. When checking a specific function passing by the second parameter $callback, the priority of that hook is returned or false if the function is not attached.

Lets see an example:
I developed a plugin name ‘Remove Add to Cart Button for WooCommerce'(https://wordpress.org/plugins/remove-add-to-cart-button-for-woocommerce/).

In that plugin, I called a callback function with plugins_loaded action hook to check if WooCommerce is active or not before running my plugin function.

add_action( 'plugins_loaded', 'ratcw_remove_add_to_cart_button_install', 12 );
function ratcw_remove_add_to_cart_button_install(){
	if ( !function_exists( 'WC' ) ) {
		add_action( 'admin_notices', 'ratcw_remove_add_to_cart_button_admin_notice' );
	} else {


So now, if we call the has_action() function like :


It will return the priority of this callback function.

Here we can see the output is 12, which is the priority of that callback function.


Retrieves the number of times an action has been fired during the current request. Let’s describe with an example:

If you take a look at the WooCommerce cart template file, you can see an action hook point is created there. Here is the code :

do_action( 'woocommerce_after_cart_item_name', $cart_item, $cart_item_key );

It calls all callback functions attached to that hook(woocommerce_after_cart_item_name).

So now, if I add a callback function to woocommerce_after_cart_item_name using the add_action() function and add some product to the cart and visit the cart page, there we will see how many times woocommerce_after_cart_item_name is called using did_action(‘woocommerce_after_cart_item_name’); function.

If you have 3 products on your cart, you will see woocommerce_after_cart_item_name called 3 times.

Please check the screenshot below for more details:

did_action example

Leave a Reply

Your email address will not be published. Required fields are marked *