【WP函数】add_action()

add_action() 是 WordPress 中心代码履行期间或特定事情发生时启动的钩子函数。 插件能够指定运用 Action API 在这些特定点上履行其一个或多个PHP函数。简单来说就是通过 add_action() 将函数衔接到指定 action 上。add_action() 的调用方法如下:

add_action( string $tag, callable $function_to_add, int $priority = 10,int $accepted_args = 1 )

$tag:必填(字符串)。$function_to_add 所挂载的动作(action)的称谓。

$function_to_add:必填(回调)。你希望挂载的函数的称谓。

$priority:可选(整型)。用于指定与特定的动作相关联的函数的履行次第。数字越小,履行越早,默许值 10 。

$accepted_args:可选(整型)。挂钩函数所接受的参数数量。

该函数定义在 wp-includes/plugin.php 文件中:

function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
    return add_filter($tag, $function_to_add, $priority, $accepted_args);
}

通过源代码可知,其实践上仍是调用了 add_filter() 函数,关于该函数请看这儿>>> WordPress学习——add_filter()详解 。

参考文档:https://developer.wordpress.org/reference/functions/add_action/