Usage

Parameters

$hook

(string) (required) The name of the action to which $function_to_add is hooked. (See Plugin API/Action Reference for a list of action hooks). Can also be the name of an action inside a theme or plugin file, or the special tag "all", in which case the function will be called for all hooks.

(int) (optional) Used to specify the order in which the functions associated with a particular action are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.

Default: 10

$accepted_args

(int) (optional) The number of arguments the hooked function accepts. In WordPress 1.5.1+, hooked functions can take extra arguments that are set when the matching do_action() or apply_filters() call is run. For example, the action comment_id_not_found will pass any functions that hook onto it the ID of the requested comment.

Accepted Arguments

A hooked function can optionally accept arguments from the action call, if any are set to be passed. In this simplistic example, the echo_comment_id function takes the $comment_id argument, which is automatically passed to when the do_action() call using the comment_id_not_found filter hook is run.

Using with a Class

To use add_action() when your plugin or theme is built using classes, you need to use the array callable syntax. You would pass the function to add_action() as an array, with $this as the first element, then the name of the class method, like so:

Notes

To find out the number and name of arguments for an action, simply search the code base for the matching do_action() call. For example, if you are hooking into 'save_post', you would find it in post.php:
<?php do_action( 'save_post', $post_ID, $post, $update ); ?>