-
-
Save aaimaawebs/78b7282f958d0e21b4551c4e4fa119e4 to your computer and use it in GitHub Desktop.
WordPress Remove Filter (remove_filter converted to remove_class_filter) to remove Filter/Action without Class Object access. Works with WordPress 1.2+ (4.7+ support added 9-19-2016)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Remove Class Filter Without Access to Class Object | |
| * | |
| * In order to use the core WordPress remove_filter() on a filter added with the callback | |
| * to a class, you either have to have access to that class object, or it has to be a call | |
| * to a static method. This method allows you to remove filters with a class callback | |
| * without having access to the object. | |
| * | |
| * | |
| * @since 1.0.0 | |
| * | |
| * @param string $tag Filter to remove | |
| * @param string $class_name Class name for the filter's callback | |
| * @param string $method_name Method name for the filter's callback | |
| * @param int $priority Priority of the filter (default 10) | |
| * | |
| * @return bool | |
| */ | |
| function remove_class_filter( $tag, $class_name = '', $method_name = '', $priority = 10 ){ | |
| global $wp_filter; | |
| // Check for filter and specific priority (default 10) | |
| if( ! isset( $wp_filter[ $tag ][ $priority ] ) || ! is_array( $wp_filter[ $tag ][ $priority ] ) ) return FALSE; | |
| // Check each filter with specified priority | |
| foreach( (array) $wp_filter[ $tag ][ $priority ] as $filter_id => $filter ) { | |
| // Filter should always be an array - array( $this, 'method' ), if not goto next | |
| if( ! isset( $filter['function'] ) || ! is_array( $filter['function'] ) ) continue; | |
| // If first value in array is not an object, it can't be a class | |
| if( ! is_object( $filter['function'][0] ) ) continue; | |
| // If class name does not match the class we're looking for, goto next | |
| if( get_class( $filter['function'][0] ) !== $class_name ) continue; | |
| // Yay we found our filter | |
| if( $filter['function'][1] === $method_name ) { | |
| // Now let's remove it from the array | |
| unset( $wp_filter[ $tag ][ $priority ][ $filter_id ] ); | |
| // and if it was the only filter in that priority, unset that priority | |
| if( empty($wp_filter[ $tag ][ $priority ]) ) unset($wp_filter[ $tag ][ $priority ]); | |
| // and if the only filter for that tag, set the tag to an empty array | |
| if( empty($wp_filter[ $tag ]) ) $wp_filter[ $tag ] = array(); | |
| // Remove this filter from merged_filters (specifies if filters have been sorted) | |
| unset($GLOBALS['merged_filters'][ $tag ]); | |
| return TRUE; | |
| } | |
| } | |
| return FALSE; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment