doing_action()
云策文档标注
概述
doing_action() 函数用于检测指定的动作钩子是否正在执行中,它基于 doing_filter() 实现,返回布尔值。此函数有助于在钩子回调中验证任何当前执行的动作,而不仅仅是最近触发的动作。
关键要点
- 函数返回布尔值,指示指定动作钩子是否正在执行栈中
- 参数 $hook_name 可选,默认为 null,用于检查特定钩子;若为 null 则检查是否有任何动作正在运行
- 与 current_action() 和 did_action() 相关,但功能不同:current_action() 返回最近执行的动作,did_action() 返回动作触发次数
- 函数内部调用 doing_filter(),实现与过滤器钩子检测类似
代码示例
// 检查 'save_post' 动作是否正在执行
if ( doing_action( 'save_post' ) ) {
// 在此处执行相关操作
}
// 检查是否有任何动作正在执行
if ( doing_action() ) {
// 在此处执行相关操作
}
原文内容
Returns whether or not an action hook is currently being processed.
Description
The function current_action() only returns the most recent action being executed.
did_action() returns the number of times an action has been fired during the current request.
This function allows detection for any action currently being executed (regardless of whether it’s the most recent action to fire, in the case of hooks called from hook callbacks) to be verified.
See also
Parameters
$hook_namestring|nulloptional-
Action hook to check. Defaults to null, which checks if any action is currently being run.
Default:
null
Source
function doing_action( $hook_name = null ) {
return doing_filter( $hook_name );
}
Changelog
| Version | Description |
|---|---|
| 3.9.0 | Introduced. |
Skip to note 2 content
Codex
Check whether the ‘save_post’ action is being executed
if ( doing_action( 'save_post' ) ) { // Do awesome here. }Check if any action is being executed
if ( doing_action() ) { // Do awesome here. }