do_action() 是WordPress的中心函数,该函数没有返回值。作用是创立一个钩子,在特定的中央执行插件或者主题开发者挂载的函数,一般存在于某个特殊的节点或者事情上。do_action() 的调用方法如下:
do_action( string $tag, $arg = \'\' )
$tag:必填(字符串)。将要被指向动作的函数称谓。
$arg:选填(混合型)。传送给钩子函数的其他参数。 默许为空。
该函数定义在 wp-includes/plugin.php 文件中:
function do_action($tag, $arg = \'\') { global $wp_filter, $wp_actions, $wp_current_filter; if ( ! isset($wp_actions[$tag]) ) $wp_actions[$tag] = 1; else ++$wp_actions[$tag]; // Do \'all\' actions first if ( isset($wp_filter[\'all\']) ) { $wp_current_filter[] = $tag; $all_args = func_get_args(); _wp_call_all_hook($all_args); } if ( !isset($wp_filter[$tag]) ) { if ( isset($wp_filter[\'all\']) ) array_pop($wp_current_filter); return; } if ( !isset($wp_filter[\'all\']) ) $wp_current_filter[] = $tag; $args = array(); if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) // array(&$this) $args[] =& $arg[0]; else $args[] = $arg; for ( $a = 2, $num = func_num_args(); $a do_action( $args ); array_pop($wp_current_filter); }
参阅文档:https://developer.wordpress.org/reference/functions/do_action/