remove_all_filters()
云策文档标注
概述
remove_all_filters() 函数用于从指定的过滤器钩子中移除所有回调函数,支持可选优先级参数以移除特定优先级的回调。
关键要点
- 函数参数:$hook_name(必需,字符串类型,指定要移除回调的过滤器钩子名称),$priority(可选,整数或false,默认false,指定要移除回调的优先级)
- 返回值:始终返回 true
- 内部机制:通过全局变量 $wp_filter 操作,移除回调后如果钩子无剩余过滤器,则从 $wp_filter 中取消设置
- 相关函数:remove_all_actions() 用于动作钩子,类似功能
- 版本历史:自 WordPress 2.7.0 引入
代码示例
remove_all_filters( 'the_content' );
remove_all_filters( 'wp_delete_file', 10 );
remove_all_filters( 'wp_delete_file', 15 );注意事项
- 移除所有回调可能影响插件或主题功能,需谨慎使用
- 优先级参数可用于移除特定优先级的回调,默认优先级通常为10
- 用户贡献笔记中示例 remove_all_filters('the_content', 'plugin_filters') 有误,第二个参数应为优先级数字而非字符串
原文内容
Removes all of the callback functions from a filter hook.
Parameters
$hook_namestringrequired-
The filter to remove callbacks from.
$priorityint|falseoptional-
The priority number to remove them from.
Default:
false
Source
function remove_all_filters( $hook_name, $priority = false ) {
global $wp_filter;
if ( isset( $wp_filter[ $hook_name ] ) ) {
$wp_filter[ $hook_name ]->remove_all_filters( $priority );
if ( ! $wp_filter[ $hook_name ]->has_filters() ) {
unset( $wp_filter[ $hook_name ] );
}
}
return true;
}
Changelog
| Version | Description |
|---|---|
| 2.7.0 | Introduced. |
Skip to note 3 content
nosilver4u
Example:
remove_all_filters( 'the_content' );This example will remove all hooks from the_content function, for any plugin or theme.
But if you only want to remove a particular set of hooks at a particular priority, you can use a priority, 10 being the default used for most filters:
remove_all_filters( 'wp_delete_file', 10 );Since class-derived filters can be tricky to remove, if they use a non-default priority (take 15 for example), you could do this instead:
remove_all_filters( 'wp_delete_file', 15 );Skip to note 4 content
Morteza Geransayeh
Example:
remove_all_filters('the_content', 'plugin_filters');This example will remove all of the plugins hooks from the_content function.