do_action

Execute functions hooked on a specific action hook.

Description

do_action( (string) $tag, (string) $arg = '' ); 

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.

You can pass extra arguments to the hooks, much like you can with apply_filters().

Parameters (2)

0. $tag (string)
The name of the action to be executed.
1. $arg — Optional. (string) => ''
Additional arguments which are passed on to the functions hooked to the action. Default empty.

Usage

  1. if ( !function_exists( 'do_action' ) ) { 
  2. require_once ABSPATH . WPINC . '/plugin.php'; 
  3.  
  4. // The name of the action to be executed. 
  5. $tag = ''; 
  6.  
  7. // Optional. Additional arguments which are passed on to the 
  8. // functions hooked to the action. Default empty. 
  9. $arg = ''; 
  10.  
  11. // NOTICE! Understand what this does before running. 
  12. $result = do_action($tag, $arg); 
  13.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/plugin.php  
  1. function do_action($tag, $arg = '') { 
  2. global $wp_filter, $wp_actions, $wp_current_filter; 
  3.  
  4. if ( ! isset($wp_actions[$tag]) ) 
  5. $wp_actions[$tag] = 1; 
  6. else 
  7. ++$wp_actions[$tag]; 
  8.  
  9. // Do 'all' actions first 
  10. if ( isset($wp_filter['all']) ) { 
  11. $wp_current_filter[] = $tag; 
  12. $all_args = func_get_args(); 
  13. _wp_call_all_hook($all_args); 
  14.  
  15. if ( !isset($wp_filter[$tag]) ) { 
  16. if ( isset($wp_filter['all']) ) 
  17. array_pop($wp_current_filter); 
  18. return; 
  19.  
  20. if ( !isset($wp_filter['all']) ) 
  21. $wp_current_filter[] = $tag; 
  22.  
  23. $args = array(); 
  24. if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) // array(&$this) 
  25. $args[] =& $arg[0]; 
  26. else 
  27. $args[] = $arg; 
  28. for ( $a = 2, $num = func_num_args(); $a < $num; $a++ ) 
  29. $args[] = func_get_arg($a); 
  30.  
  31. $wp_filter[ $tag ]->do_action( $args ); 
  32.  
  33. array_pop($wp_current_filter);