{"id":620,"date":"2022-10-28T14:36:36","date_gmt":"2022-10-28T14:36:36","guid":{"rendered":"https:\/\/demo.wpartisan.net\/developmentsite\/has_action-vs-did_action-in-wordpress\/"},"modified":"2024-11-04T04:24:19","modified_gmt":"2024-11-04T04:24:19","slug":"has_action-vs-did_action-in-wordpress","status":"publish","type":"post","link":"https:\/\/demo.wpartisan.net\/developmentsite\/has_action-vs-did_action-in-wordpress\/","title":{"rendered":"has_action VS did_action in WordPress"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">has_action<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Basically, We use the has_action() function to check if any action( callback ) has been registered for a hook.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Lets see an example:<\/strong><br>I developed a plugin name &#8216;Remove Add to Cart Button for WooCommerce'(https:\/\/wordpress.org\/plugins\/remove-add-to-cart-button-for-woocommerce\/).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<pre class=\"wp-block-code \\&quot;wp-block-code\\&quot;\"><code lang=\"\\&quot;php\\&quot;\" class=\"language-\\&quot;php\\&quot;\">add_action( 'plugins_loaded', 'ratcw_remove_add_to_cart_button_install', 12 );\nfunction ratcw_remove_add_to_cart_button_install(){\n\tif ( !function_exists( 'WC' ) ) {\n\t\tadd_action( 'admin_notices', 'ratcw_remove_add_to_cart_button_admin_notice' );\n\t} else {\n\t\tratcw_run_remove_add_to_cart_button_woocommerce();\n\t}\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So now, if we call the has_action() function like : <\/p>\n\n\n\n<pre class=\"wp-block-code \\&quot;wp-block-code\\&quot;\"><code lang=\"\\&quot;php\\&quot;\" class=\"language-\\&quot;php\\&quot;\">var_dump(has_action('plugins_loaded','ratcw_remove_add_to_cart_button_install'));<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">It will return the priority of this callback function. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here we can see the output is 12, which is the priority of that callback function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">did_action<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Retrieves the number of times an action has been fired during the current request. Let&#8217;s describe with an example:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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 :<\/p>\n\n\n\n<pre class=\"wp-block-code \\&quot;wp-block-code\\&quot;\"><code lang=\"\\&quot;php\\&quot;\" class=\"language-\\&quot;php\\&quot;\">do_action( 'woocommerce_after_cart_item_name', $cart_item, $cart_item_key );<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">It calls all callback functions attached to that hook(woocommerce_after_cart_item_name).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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(&#8216;woocommerce_after_cart_item_name&#8217;); function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you have 3 products on your cart, you will see woocommerce_after_cart_item_name called 3 times.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Please check the screenshot below for more details:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large \\&quot;wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"632\" src=\"https:\/\/demo.wpartisan.net\/developmentsite\/wp-content\/uploads\/2022\/10\/did_action-output-1-1024x632.png\" alt=\"\" class=\"wp-image-2289\" srcset=\"https:\/\/demo.wpartisan.net\/developmentsite\/wp-content\/uploads\/2022\/10\/did_action-output-1-1024x632.png 1024w, https:\/\/demo.wpartisan.net\/developmentsite\/wp-content\/uploads\/2022\/10\/did_action-output-1-300x185.png 300w, https:\/\/demo.wpartisan.net\/developmentsite\/wp-content\/uploads\/2022\/10\/did_action-output-1-768x474.png 768w, https:\/\/demo.wpartisan.net\/developmentsite\/wp-content\/uploads\/2022\/10\/did_action-output-1.png 1364w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will learn what has_action and did_action are and how we can use them. Here we can see everything about has_action and did_action with an example.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[18],"tags":[40,41],"class_list":["post-620","post","type-post","status-publish","format-standard","hentry","category-wordpress","tag-action-hooks","tag-filter-hooks"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"WPArtisan","author_link":"https:\/\/demo.wpartisan.net\/developmentsite\/author\/developmentsiteauth\/"},"uagb_comment_info":0,"uagb_excerpt":"In this article, we will learn what has_action and did_action are and how we can use them. Here we can see everything about has_action and did_action with an example.","_links":{"self":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/posts\/620","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/comments?post=620"}],"version-history":[{"count":1,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/posts\/620\/revisions"}],"predecessor-version":[{"id":2290,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/posts\/620\/revisions\/2290"}],"wp:attachment":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/media?parent=620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/categories?post=620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/tags?post=620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}