函数文档

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

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

Return

bool|int If $callback is omitted, returns boolean for whether the hook has anything registered. When checking a specific function, the priority of that hook is returned, or false if the function is not attached.
If $callback and $priority are both provided, a boolean is returned for whether the specific function is registered at that priority.

More Information

Since this action is an alias of has_filter() , it also uses the global array $wp_filter that stores all of the filters / actions.

Source

function has_action( $hook_name, $callback = false, $priority = false ) {
	return has_filter( $hook_name, $callback, $priority );
}

Changelog

Version Description
6.9.0 Added the $priority parameter.
2.5.0 Introduced.