remove_class_filter

Remove Class Filter Without Access to Class Object.

Description

remove_class_filter( (string) $tag, (string) $class_name = '', (string) $method_name = '', (int) $priority = 10 ); 

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 callback to a class you don't have access to.

Works with WordPress 1.2 - 4.7+

Parameters (4)

0. $tag (string)
Filter to remove
1. $class_name — Optional. (string) => ''
Class name for the filter's callback
2. $method_name — Optional. (string) => ''
Method name for the filter's callback
3. $priority — Optional. (int) => 10
Priority of the filter (default 10)

Usage

  1. if ( !function_exists( 'remove_class_filter' ) ) { 
  2. require_once '/includes/helpers.php'; 
  3.  
  4. // Filter to remove 
  5. $tag = ''; 
  6.  
  7. // Class name for the filter's callback 
  8. $class_name = ''; 
  9.  
  10. // Method name for the filter's callback 
  11. $method_name = ''; 
  12.  
  13. // Priority of the filter (default 10) 
  14. $priority = 10; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = remove_class_filter($tag, $class_name, $method_name, $priority); 
  18.  

Defined (1)

The function is defined in the following location(s).

/includes/helpers.php  
  1. function remove_class_filter( $tag, $class_name = '', $method_name = '', $priority = 10 ) { 
  2. global $wp_filter; 
  3. // Check that filter actually exists first 
  4. if ( ! isset( $wp_filter[ $tag ] ) ) return FALSE; 
  5. /** 
  6. * If filter config is an object, means we're using WordPress 4.7+ and the config is no longer 
  7. * a simple array, rather it is an object that implements the ArrayAccess interface. 
  8. * To be backwards compatible, we set $callbacks equal to the correct array as a reference (so $wp_filter is updated) 
  9. * @see https://make.wordpress.org/core/2016/09/08/wp_hook-next-generation-actions-and-filters/ 
  10. */ 
  11. if ( is_object( $wp_filter[ $tag ] ) && isset( $wp_filter[ $tag ]->callbacks ) ) { 
  12. $callbacks = &$wp_filter[ $tag ]->callbacks; 
  13. } else { 
  14. $callbacks = &$wp_filter[ $tag ]; 
  15. // Exit if there aren't any callbacks for specified priority 
  16. if ( ! isset( $callbacks[ $priority ] ) || empty( $callbacks[ $priority ] ) ) return FALSE; 
  17. // Loop through each filter for the specified priority, looking for our class & method 
  18. foreach( (array) $callbacks[ $priority ] as $filter_id => $filter ) { 
  19. // Filter should always be an array - array( $this, 'method' ), if not goto next 
  20. if ( ! isset( $filter[ 'function' ] ) || ! is_array( $filter[ 'function' ] ) ) continue; 
  21. // If first value in array is not an object, it can't be a class 
  22. if ( ! is_object( $filter[ 'function' ][ 0 ] ) ) continue; 
  23. // Method doesn't match the one we're looking for, goto next 
  24. if ( $filter[ 'function' ][ 1 ] !== $method_name ) continue; 
  25. // Method matched, now let's check the Class 
  26. if ( get_class( $filter[ 'function' ][ 0 ] ) === $class_name ) { 
  27. // Now let's remove it from the array 
  28. unset( $callbacks[ $priority ][ $filter_id ] ); 
  29. // and if it was the only filter in that priority, unset that priority 
  30. if ( empty( $callbacks[ $priority ] ) ) unset( $callbacks[ $priority ] ); 
  31. // and if the only filter for that tag, set the tag to an empty array 
  32. if ( empty( $callbacks ) ) $callbacks = array(); 
  33. // If using WordPress older than 4.7 
  34. if ( ! is_object( $wp_filter[ $tag ] ) ) { 
  35. // Remove this filter from merged_filters, which specifies if filters have been sorted 
  36. unset( $GLOBALS[ 'merged_filters' ][ $tag ] ); 
  37. return TRUE; 
  38. return FALSE;