remove_filter()
Removes a function from a specified filter hook.
This function can be used to remove default functions attached to a specific filter hook and possibly replace them with a substitute or to remove hooks that are added by plugins but interfere (conflict) with current code.
IMPORTANT: When removing a hook, the priority ($priority) must match the one that was set when the hook was created. If it does not match, you will not see any warnings about this - the function will simply return false.
Notes
- It's impossible to remove a filter before it has been added!
- If you want to delete a hook that was created inside a class, then you need to pass this instance of the class from which the hook was created.
No Hooks.
Returns
true|false. Whether the function existed before it was removed.
Usage
remove_filter( $hook_name, $callback, $priority );
- $hook_name(string) (required)
- The filter hook to which the function to be removed is hooked.
- $callback(callable|string|array) (required)
- The callback to be removed from running when the filter is applied. This function can be called unconditionally to speculatively remove a callback that may or may not exist.
- $priority(int)
- The exact priority used when adding the original filter callback.
Default:10
Examples
#1 Remove a filter wpautop from hook the_content
remove_filter( 'the_content', 'wpautop' );
#2 Remove a filter capital_P_dangit from hooks 'the_content', 'the_title', 'comment_text'
foreach ( array( 'the_content', 'the_title', 'comment_text' ) as $hook ) {
remove_filter( $hook, 'capital_P_dangit' );
} #3 Remove a filter added from PHP class
If a filter has been added from within a class, removing it will require accessing the class variable:
global $my_class; remove_filter( 'the_content', array( $my_class, 'class_filter_function') );
To get a better grasp of removing hooks that use class method, see the description of remove_action()
#4 More Examples
See examples of remove_action().
Notes
- Global. WP_Hook[].
$wp_filterStores all of the filters and actions.
Changelog
| Since 1.2.0 | Introduced. |
remove_filter() remove filter code WP 7.0
function remove_filter( $hook_name, $callback, $priority = 10 ) {
global $wp_filter;
$r = false;
if ( isset( $wp_filter[ $hook_name ] ) ) {
$r = $wp_filter[ $hook_name ]->remove_filter( $hook_name, $callback, $priority );
if ( ! $wp_filter[ $hook_name ]->callbacks ) {
unset( $wp_filter[ $hook_name ] );
}
}
return $r;
}