has_action VS did_action in WordPress


Basically we used the has_action() function to checks if any action( callback ) has been registered for a hook.

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

Here first parameter $hook_name is required. Means you must need to pass the $hook_name with this function. Second parameter $callback is optional.

If $callback parameter is not passed, returns boolean for whether the hook has anything registered. When checking a specific function passing by 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 run 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 : var_dump(has_action(‘plugins_loaded’, ‘ratcw_remove_add_to_cart_button_install’));, it return the priority of this call back 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. Lets describe with an example:

If you take a look on woocommerce cart template file, you can see a action hook is create there. Here is code :

do_action( ‘woocommerce_after_cart_item_name’, $cart_item, $cart_item_key );

It calls the all callback function which are attached to that hook name (woocommerce_after_cart_item_name).

So now if i add a callback function to woocommerce_after_cart_item_name using add_action() function and add some product to 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 you cart, then you will see woocommerce_after_cart_item_name called 3 times.

Please check the screenshot below for more details:

In this article, we will learn what is has_action and did_action and how can we use it. We can see all the things about has_action and did_action with an example.

Leave a Reply

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