Understanding do_action() and add_action() – Revisited
While designing your themes, often times you want to be able to push dynamic content to a particular area of it. The do_action(), add_action() functions of WP are a great tool to use.
These functions in wordpress allow you to specify a point in the code to trigger or initiate another segment of code. Like an extension to the original code that’s placed there. This is used so that different plugins that are created can use those ‘hooks’ in the wordpress code to add to the original core code without having to actually edit the core code itself.
do_action()
http://codex.wordpress.org/Function_Reference/do_action
The WordPress.ORG Codex refers to the usage as:
do_action($tag, $arg = );
The $tag refers to the ‘Label’ you specify to hook into; with your add_action function. The $arg = refers to the arguments that you want to pass through the do_action and into the add_action.
.. That really doesn’t say much on how to actually use the do_action. What the do_action() function does is set’s a label in a specific point in your code for your plugin or theme code to hook into with the add_action() function.
For example, I place a do_action() right above the blog area of my theme.
do_action('mytheme_aboveblog');
What you’re doing when you add the do_action is make it so that at that place in your code you can inject some other code into it. What makes this different then actually hardcoding the function name there? Using the do_action() allows multiple functions to access that particular point in the place you put it at, not just one, which you can use to set different events to occur to trigger them to display or be used there.
add_action()
http://codex.wordpress.org/Function_Reference/add_action
The WordPress.ORG Codex refers to the usage as:
add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1);
The $tag refers to the label you specified as the hook in the do_action() function. $function_to_add refers to what the name of the function is inside your plugin to execute, $priority refers to how much of a priority is given to execution of this function and $accepted_args refers to the number of arguments passed through do_action() to the $function_to_add.
What this is really saying? The add_action finds the label specified in the $tag and hooks into it saying to that bit of code to “execute this function in addition to what you’re already doing, oh and pass these arguments.” For example, in my activity monitor plugin:
add_action('mytheme_aboveblog', 'mytheme_online_user_message');
Now, whenever the do_action checks for something to be injected into it to be used the add_action will trigger the function mytheme_online_user_message to run.
function mytheme_online_user_message() { global $current_user; if (!empty($current_user)) { $user_login = addslashes($current_user->user_login); echo "Hello {$user_login}!"; } }
Now if the user is logged on it will run this function in that do_action place you put it and if the user is online inject that message into that place.
Summary
Placing a do_action() into your code allows other functions to hook into it to extend the functionality of where that hook is placed, using add_action().
Thank you so much in explaining the difference between do_action and add_action – your explanation is better then the one on Codex.
After I read your explanation, I understand it better than document from Codex. I has some question about do action.
I use following code : do_action(‘activate_’ . $plugin);
for activate my blog plugin but it seem does not work (My multisite blogs). Do I miss anything ?
nice explanation
nice explanation
This is soooo good. codex never actually explains anything it just lists 🙁
Super………….. greeat!!!!!!!!!!
Thank you Philip!
Your post is four years old but still so useful.
Great explanation.
can I delete that code in my theme if its not useful at all?
Yes. If the injection at that location doesn’t do anything important for your theme; not necessary in being there. However, if another plugin or situation comes along that you might want it; if it’s deleted it won’t be there.
Thanks. It is really inciting!
Thanks ! its very useful for me….
It’s awasome explanation great job
Thanks