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, $merged_filters, $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. // Sort 
  32. if ( !isset( $merged_filters[ $tag ] ) ) { 
  33. ksort($wp_filter[$tag]); 
  34. $merged_filters[ $tag ] = true; 
  35.  
  36. reset( $wp_filter[ $tag ] ); 
  37.  
  38. do { 
  39. foreach ( (array) current($wp_filter[$tag]) as $the_ ) 
  40. if ( !is_null($the_['function']) ) 
  41. call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args'])); 
  42.  
  43. } while ( next($wp_filter[$tag]) !== false ); 
  44.  
  45. array_pop($wp_current_filter);