remove_filter()删除一个过滤器的回调方法

目录

描述

译文

该函数移除一个附属于指定过滤器hook的函数。该方法可用来移除附属于特定过滤器hook的默认函数,并可能用其它函数取而代之。参见rremove_action(), add_filter()和add_action()。

重要:添加hook时的$function_to_remove 和$priority参数要能够相匹配,这样才可以移除hook。该原则也适用于过滤器和动作。移除失败时不进行警告提示。

原文

This function removes a function attached to a specified filter hook. This method can be used to remove default functions attached to a specific filter hook and possibly replace them with a substitute. See also remove_action(), add_filter() and add_action().

Important: To remove a hook, the $function_to_remove and $priority arguments must match when the hook was added. This goes for both filters and actions. No warning will be given on removal failure.

remove_filter() 描述

用法

<?php remove_filter$tag$function_to_remove$priority ); ?>

remove_filter() 用法

参数

$tag

(string) (必填) The action hook to which the function to be removed is hooked.

默认值: None

$function_to_remove

(callback) (必填) The callback for the function which should be removed.

默认值: None

$priority

(int) (可选) The priority of the function (as defined when the function was originally hooked).

默认值: 10

remove_filter() 参数

注意

When using 过滤器s passed with objects, you most often need to pass the exact same object back to the remove 过滤器 call, not just another instance of that object.(read _wp_filter_build_unique_id() for the gory details)

remove_filter() 注意

历史

  • 添加于 版本: 1.2.0

remove_filter() 历史

源文件

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

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Removes a function from a specified filter hook.
 *
 * This function removes a function attached to a specified filter hook. This
 * method can be used to remove default functions attached to a specific filter
 * hook and possibly replace them with a substitute.
 *
 * To remove a hook, the $function_to_remove and $priority arguments must match
 * when the hook was added. This goes for both filters and actions. No warning
 * will be given on removal failure.
 *
 * @since 1.2.0
 *
 * @global array $wp_filter         Stores all of the filters
 * @global array $merged_filters    Merges the filter hooks using this function.
 *
 * @param string   $tag                The filter hook to which the function to be removed is hooked.
 * @param callback $function_to_remove The name of the function which should be removed.
 * @param int      $priority           Optional. The priority of the function. Default 10.
 * @return bool    Whether the function existed before it was removed.
 */
function remove_filter( $tag, $function_to_remove, $priority = 10 ) {
	$function_to_remove = _wp_filter_build_unique_id( $tag, $function_to_remove, $priority );

	$r = isset( $GLOBALS['wp_filter'][ $tag ][ $priority ][ $function_to_remove ] );

	if ( true === $r ) {
		unset( $GLOBALS['wp_filter'][ $tag ][ $priority ][ $function_to_remove ] );
		if ( empty( $GLOBALS['wp_filter'][ $tag ][ $priority ] ) ) {
			unset( $GLOBALS['wp_filter'][ $tag ][ $priority ] );
		}
		if ( empty( $GLOBALS['wp_filter'][ $tag ] ) ) {
			$GLOBALS['wp_filter'][ $tag ] = array();
		}
		unset( $GLOBALS['merged_filters'][ $tag ] );
	}

	return $r;
}

remove_filter() 源文件

相关