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.


The WordPress.ORG Codex refers to the usage as:

do_action($tag, $arg = );

The $tag refers to the ‘Label’ you specify took 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.


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.


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.


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().