has_filter()判断一个过滤器钩子是否注册了某个回调方法

目录

描述

Check if any filter has been registered for a hook.

has_filter() 描述

用法

 <?php has_filter$tag$function_to_check ); ?> 

has_filter() 用法

参数

$tag

(string) (必填) The name of the filter hook.

默认值: None

$function_to_check

(callback) (可选) If specified, return the priority of that function on this hook. If the specified function is not attached to this hook, returns false.

默认值: False

has_filter() 参数

示例


<?php
if ( ! has_filter'the_content''example_alter_the_content' ) )
    
add_filter'the_content''prefix_alter_the_content' );
?>

has_filter() 示例

注意

  • 使用到: global array $wp_filter that stores all of the 过滤器s

has_filter() 注意

历史

  • 添加于 版本: 2.5.0

has_filter() 历史

源文件

has_filter() 函数的代码位于 wp-includes/plugin.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Check if any filter has been registered for a hook.
 *
 * @since 2.5.0
 *
 * @global array $wp_filter Stores all of the filters.
 *
 * @param string        $tag               The name of the filter hook.
 * @param callback|bool $function_to_check Optional. The callback to check for. Default false.
 * @return false|int If $function_to_check 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. When using the
 *                   $function_to_check 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.
 */
function has_filter($tag, $function_to_check = false) {
	// Don't reset the internal array pointer
	$wp_filter = $GLOBALS['wp_filter'];

	$has = ! empty( $wp_filter[ $tag ] );

	// Make sure at least one priority has a filter callback
	if ( $has ) {
		$exists = false;
		foreach ( $wp_filter[ $tag ] as $callbacks ) {
			if ( ! empty( $callbacks ) ) {
				$exists = true;
				break;
			}
		}

		if ( ! $exists ) {
			$has = false;
		}
	}

	if ( false === $function_to_check || false === $has )
		return $has;

	if ( !$idx = _wp_filter_build_unique_id($tag, $function_to_check, false) )
		return false;

	foreach ( (array) array_keys($wp_filter[$tag]) as $priority ) {
		if ( isset($wp_filter[$tag][$priority][$idx]) )
			return $priority;
	}

	return false;
}

has_filter() 源文件

相关