remove_filter()删除过滤器

功能:

remove_filter()函数用来从过滤器钩子中删除一个函数

用法:

<?php remove_filter( $tag, $function_to_remove, $priority = 10 );?>

参数:

$tag

(字符串)(必需)将要被删除的函数所连接到的动作hook。

默认值:None

$function_to_remove

(回调)(必需) 将要被删除函数的名称

默认值:None

$priority

(整数)(可选)函数优先级(在函数最初连接时定义)

默认值:10

$accepted_args

(整数)(必需)函数所接受参数的数量。

默认值:1

返回值:

(布尔值)函数是否被移除。

所在位置:

remove_filter()函数包含在wp-includes/plugin.php中.

源码:

/**
 * 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
 *
 * @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 boolean 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;
}

示例:

function test_func( $text, $var1, $var2 ){
    return $text . $var1 . $var2;
}
echo remove_filter( 'test', 'test_func' );

相关函数:

add_filter();
apply_filters();

(完)

版权声明WordPress头条正在使用WordPress文章版权插件
如未注明,均为原创,转载需注明出处
本文链接地址:http://www.wptoutiao.com/functions/69.html

过滤器的相关文章

钩子的相关文章

还可以分享文章给好友:

发表评论

电子邮件地址不会被公开。 必填项已用*标注