函数文档

did_filter()

💡 云策文档标注

概述

did_filter() 函数用于获取在当前请求中特定过滤器钩子被应用的次数。它通过检查全局变量 $wp_filters 来实现,如果钩子未设置则返回 0。

关键要点

  • 函数参数:$hook_name(字符串,必需),指定过滤器钩子的名称。
  • 返回值:整数,表示过滤器钩子被应用的次数。
  • 内部逻辑:检查全局 $wp_filters 数组,若钩子不存在则返回 0,否则返回其值。

代码示例

function did_filter( $hook_name ) {
	global $wp_filters;

	if ( ! isset( $wp_filters[ $hook_name ] ) ) {
		return 0;
	}

	return $wp_filters[ $hook_name ];
}

注意事项

此函数在 WordPress 6.1.0 版本中引入,适用于调试和监控过滤器钩子的使用情况。


📄 原文内容

Retrieves the number of times a filter has been applied during the current request.

Parameters

$hook_namestringrequired
The name of the filter hook.

Return

int The number of times the filter hook has been applied.

Source

function did_filter( $hook_name ) {
	global $wp_filters;

	if ( ! isset( $wp_filters[ $hook_name ] ) ) {
		return 0;
	}

	return $wp_filters[ $hook_name ];
}

Changelog

Version Description
6.1.0 Introduced.