add_action($tag, $function_to_add, $priority, $accepted_args)
add_action() attaches a function to a specific action, which is then triggered by a do_action() call somewhere during execution. This can be used to execute a group of functions using the $priority parameter to determine order (optional), and arguments can be passed through the $accepted_args parameter. Below are a few examples of how this is used in the WordPress platform:
This is executed after a post or page is deleted, and post or page ID is passed as a parameter. This allows you add custom functionality whenever a post is deleted. For example, lets say you want to be notified every time a post is deleted. You can create a function to send the e-mail, and execute add_action(‘deleted_post’, ‘notify_when_deleted’, 10, $email). After that, when do_action(‘deleted_post’) is called, the notify_when_deleted() function will be called.
The wp_head action is traditionally called within a theme’s header. If you wanted to create an SEO plugin, you could very easily add an action to wp_head to display meta tags. Continue reading 10+ WordPress Functions Every Developer Should Know About