WordPress add_action and do_action?

I bring up this topic cause these two code makes me wondering what is it for, how i am gonna use it. Of course WordPress provides the explanation about it but as always hard to understand it without the real example. And here i am trying to give out some of my insight.

These add_action and do_action can not be seperated cause these two are connected, do_action is function to run it and add_action is function to declare it.

  • add_action

add_action required 2 parameter : action name and call back function, the others are optional. You use add_action() to set some code to your theme, remember only set, it will not run before it is called and normally you put add_action inside function.php. As example you set add_action(“after_body”,”mycallback_function”), then u only put echo “welcome” inside function mycallback_function().

  • do_action

add_action required 1 parameter : action name. It’s like a hook function, you use it to call some code into your theme that already being set inside your function.php before. As example you put do_action(“after_body”) at your header.php, it is going call an action name “after_body” that we already set. And the result is an echo “welcome” at your theme header.

Okay that is my short explanation, i am trying to explain it as simple as i could. Why we use add_action and do_action? It is to extend our code, like we want to insert some code inside spesific area inside our theme.

Leave a Reply

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