has_action()
云策文档标注
概述
has_action() 函数用于检查指定的动作钩子是否已注册任何回调函数。它是 has_filter() 的别名,共享相同的实现逻辑。
关键要点
- 函数签名:has_action( $hook_name, $callback = false, $priority = false )
- 参数说明:$hook_name(必需,动作钩子名称),$callback(可选,回调函数,默认为 false),$priority(可选,优先级,默认为 false)
- 返回值:根据参数组合返回布尔值或整数,用于指示钩子注册状态或回调优先级
- 注意事项:使用 $callback 参数时,返回值可能为非布尔假值(如 0),建议使用 === 运算符进行严格比较
代码示例
function has_action( $hook_name, $callback = false, $priority = false ) {
return has_filter( $hook_name, $callback, $priority );
}注意事项
该函数依赖于全局数组 $wp_filter 来存储所有过滤器和动作,确保在调用前相关钩子已正确初始化。
原文内容
Checks if any action has been registered for a hook.
Description
When using the $callback argument, this function may return a non-boolean value that evaluates to false (e.g. 0), so use the === operator for testing the return value.
See also
- has_filter(): This function is an alias of has_filter() .
Parameters
$hook_namestringrequired-
The name of the action hook.
$callbackcallable|string|array|falseoptional-
The callback to check for.
This function can be called unconditionally to speculatively check a callback that may or may not exist.Default:
false $priorityint|falseoptional-
The specific priority at which to check for the callback.
Default:
false
Source
function has_action( $hook_name, $callback = false, $priority = false ) {
return has_filter( $hook_name, $callback, $priority );
}