Difference Between Do_Action and Add_Action

Difference between do_action() and add_action() in WordPress

While designing your themes, sometimes you want to be able to push dynamic content to a particular area of it. At this time, the do_action(), add_action() functions of WP are a great tool to use.


These function in WordPress allows you to specify a points in the code to trigger or initiate another segment of codes. It behaves similarly an extension to the original code that’s placed there. It 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()

This function invokes all functions attached to action hook $tag. It is possible to create new action hooks by simply calling this function, specifying the name of the new hook using the $tag parameter.

The WordPress.ORG Codex refers to the usage as:
do_action($tag, $arg = );
where;

$tag: The name of the action to be executed.
$arg =: The arguments that you want to pass through the do_action and into the add_action.

For example, if you add the following in my theme’s footer:

do_action(‘my_theme_footer’);

add_action()

These are the hooks that the WordPress core launches it at specific points during execution, or when some specific events occur. Plugins can specified that one or more of its PHP functions are executed at these points, using the Action API.

The WordPress.ORG Codex refers to the usage as:
add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1);
where;

$tag: The name of the action to which the $function_to_add is hooked.
$function_to_add: The name of the function you wish to be called.
$priority: Used to specifies the order in which the functions associated with a particular action that are executed.
$accepted_args: The number of arguments the function accepts.

Now, for example, when the do_action checks for something to be injected into it to be used the add_action will trigger the function my_theme_display to run.

add_action( ‘my_theme_footer’, ‘my_theme_display’ );

function my_theme_display(){

echo ‘hello world’;

}