A name for an event that is executed at specific points throughout the WordPress core.
An action is a name for an event that is executed at specific points throughout the WordPress core. This name must be a valid array key (string or number).
The event is called by using do_action( 'event-name' );
.
Example:
do_action( 'wp_footer' );
Developers can create a custom action using the Action API to add or remove code from an existing action by specifying any existing Hook. This process is called "hooking".
For example: A developer may want to add code to the footer of a theme. This could be accomplished by writing new function, then hooking it to the wp_footer
action:
add_action( 'wp_footer', 'my_custom_callback' );
function my_custom_callback()
{
echo 'Hello world!';
}
The callback function doesn’t have to return
anything, because that will be ignored by WordPress. It is sometimes useful to return a value to make unit-tests easier.
- See also the Codex: Filter, Hook, Terminology Confusion
- Related articles: Actions, Action Reference,
add_action()
- Forum posts: Filters vs. Actions Discussion and Explanation