Skip to content

Instantly share code, notes, and snippets.

@aaimaawebs
Forked from tripflex/functions.php
Created May 3, 2018 18:05
Show Gist options
  • Save aaimaawebs/78b7282f958d0e21b4551c4e4fa119e4 to your computer and use it in GitHub Desktop.
Save aaimaawebs/78b7282f958d0e21b4551c4e4fa119e4 to your computer and use it in GitHub Desktop.

Revisions

  1. @tripflex tripflex revised this gist Jun 8, 2017. 1 changed file with 104 additions and 81 deletions.
    185 changes: 104 additions & 81 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -1,106 +1,129 @@
    <?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 callback to a class
    * you don't have access to.
    *
    * Works with WordPress 1.2+ (4.7+ support added 9-19-2016)
    * Updated 2-27-2017 to use internal WordPress removal for 4.7+ (to prevent PHP warnings output)
    *
    * @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 Whether the function is removed.
    * Make sure the function does not exist before defining it
    */
    function remove_class_filter( $tag, $class_name = '', $method_name = '', $priority = 10 ) {
    global $wp_filter;

    // Check that filter actually exists first
    if ( ! isset( $wp_filter[ $tag ] ) ) return FALSE;
    if( ! function_exists( 'remove_class_filter' ) ){

    /**
    * If filter config is an object, means we're using WordPress 4.7+ and the config is no longer
    * a simple array, rather it is an object that implements the ArrayAccess interface.
    * 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 callback to a class
    * you don't have access to.
    *
    * To be backwards compatible, we set $callbacks equal to the correct array as a reference (so $wp_filter is updated)
    * Works with WordPress 1.2+ (4.7+ support added 9-19-2016)
    * Updated 2-27-2017 to use internal WordPress removal for 4.7+ (to prevent PHP warnings output)
    *
    * @see https://make.wordpress.org/core/2016/09/08/wp_hook-next-generation-actions-and-filters/
    * @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 Whether the function is removed.
    */
    if ( is_object( $wp_filter[ $tag ] ) && isset( $wp_filter[ $tag ]->callbacks ) ) {
    // Create $fob object from filter tag, to use below
    $fob = $wp_filter[ $tag ];
    $callbacks = &$wp_filter[ $tag ]->callbacks;
    } else {
    $callbacks = &$wp_filter[ $tag ];
    }
    function remove_class_filter( $tag, $class_name = '', $method_name = '', $priority = 10 ) {

    // Exit if there aren't any callbacks for specified priority
    if ( ! isset( $callbacks[ $priority ] ) || empty( $callbacks[ $priority ] ) ) return FALSE;
    global $wp_filter;

    // Loop through each filter for the specified priority, looking for our class & method
    foreach( (array) $callbacks[ $priority ] as $filter_id => $filter ) {
    // Check that filter actually exists first
    if ( ! isset( $wp_filter[ $tag ] ) ) {
    return FALSE;
    }

    // Filter should always be an array - array( $this, 'method' ), if not goto next
    if ( ! isset( $filter[ 'function' ] ) || ! is_array( $filter[ 'function' ] ) ) continue;
    /**
    * If filter config is an object, means we're using WordPress 4.7+ and the config is no longer
    * a simple array, rather it is an object that implements the ArrayAccess interface.
    *
    * To be backwards compatible, we set $callbacks equal to the correct array as a reference (so $wp_filter is updated)
    *
    * @see https://make.wordpress.org/core/2016/09/08/wp_hook-next-generation-actions-and-filters/
    */
    if ( is_object( $wp_filter[ $tag ] ) && isset( $wp_filter[ $tag ]->callbacks ) ) {
    // Create $fob object from filter tag, to use below
    $fob = $wp_filter[ $tag ];
    $callbacks = &$wp_filter[ $tag ]->callbacks;
    } else {
    $callbacks = &$wp_filter[ $tag ];
    }

    // If first value in array is not an object, it can't be a class
    if ( ! is_object( $filter[ 'function' ][ 0 ] ) ) continue;
    // Exit if there aren't any callbacks for specified priority
    if ( ! isset( $callbacks[ $priority ] ) || empty( $callbacks[ $priority ] ) ) {
    return FALSE;
    }

    // Method doesn't match the one we're looking for, goto next
    if ( $filter[ 'function' ][ 1 ] !== $method_name ) continue;
    // Loop through each filter for the specified priority, looking for our class & method
    foreach ( (array) $callbacks[ $priority ] as $filter_id => $filter ) {

    // Method matched, now let's check the Class
    if ( get_class( $filter[ 'function' ][ 0 ] ) === $class_name ) {
    // Filter should always be an array - array( $this, 'method' ), if not goto next
    if ( ! isset( $filter['function'] ) || ! is_array( $filter['function'] ) ) {
    continue;
    }

    // WordPress 4.7+ use core remove_filter() since we found the class object
    if( isset( $fob ) ){
    // Handles removing filter, reseting callback priority keys mid-iteration, etc.
    $fob->remove_filter( $tag, $filter['function'], $priority );
    // If first value in array is not an object, it can't be a class
    if ( ! is_object( $filter['function'][0] ) ) {
    continue;
    }

    } else {
    // Use legacy removal process (pre 4.7)
    unset( $callbacks[ $priority ][ $filter_id ] );
    // and if it was the only filter in that priority, unset that priority
    if ( empty( $callbacks[ $priority ] ) ) {
    unset( $callbacks[ $priority ] );
    }
    // and if the only filter for that tag, set the tag to an empty array
    if ( empty( $callbacks ) ) {
    $callbacks = array();
    }
    // Remove this filter from merged_filters, which specifies if filters have been sorted
    unset( $GLOBALS['merged_filters'][ $tag ] );
    // Method doesn't match the one we're looking for, goto next
    if ( $filter['function'][1] !== $method_name ) {
    continue;
    }

    return TRUE;
    // Method matched, now let's check the Class
    if ( get_class( $filter['function'][0] ) === $class_name ) {

    // WordPress 4.7+ use core remove_filter() since we found the class object
    if ( isset( $fob ) ) {
    // Handles removing filter, reseting callback priority keys mid-iteration, etc.
    $fob->remove_filter( $tag, $filter['function'], $priority );

    } else {
    // Use legacy removal process (pre 4.7)
    unset( $callbacks[ $priority ][ $filter_id ] );
    // and if it was the only filter in that priority, unset that priority
    if ( empty( $callbacks[ $priority ] ) ) {
    unset( $callbacks[ $priority ] );
    }
    // and if the only filter for that tag, set the tag to an empty array
    if ( empty( $callbacks ) ) {
    $callbacks = array();
    }
    // Remove this filter from merged_filters, which specifies if filters have been sorted
    unset( $GLOBALS['merged_filters'][ $tag ] );
    }

    return TRUE;
    }
    }
    }

    return FALSE;
    return FALSE;
    }
    }

    /**
    * Remove Class Action Without Access to Class Object
    *
    * In order to use the core WordPress remove_action() on an action 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 actions with a callback to a class
    * you don't have access to.
    *
    * Works with WordPress 1.2+ (4.7+ support added 9-19-2016)
    *
    * @param string $tag Action to remove
    * @param string $class_name Class name for the action's callback
    * @param string $method_name Method name for the action's callback
    * @param int $priority Priority of the action (default 10)
    *
    * @return bool Whether the function is removed.
    * Make sure the function does not exist before defining it
    */
    function remove_class_action( $tag, $class_name = '', $method_name = '', $priority = 10 ) {
    remove_class_filter( $tag, $class_name, $method_name, $priority );
    if( ! function_exists( 'remove_class_action') ){

    /**
    * Remove Class Action Without Access to Class Object
    *
    * In order to use the core WordPress remove_action() on an action 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 actions with a callback to a class
    * you don't have access to.
    *
    * Works with WordPress 1.2+ (4.7+ support added 9-19-2016)
    *
    * @param string $tag Action to remove
    * @param string $class_name Class name for the action's callback
    * @param string $method_name Method name for the action's callback
    * @param int $priority Priority of the action (default 10)
    *
    * @return bool Whether the function is removed.
    */
    function remove_class_action( $tag, $class_name = '', $method_name = '', $priority = 10 ) {
    remove_class_filter( $tag, $class_name, $method_name, $priority );
    }
    }
  2. @tripflex tripflex revised this gist Feb 27, 2017. 1 changed file with 19 additions and 11 deletions.
    30 changes: 19 additions & 11 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,7 @@
    * you don't have access to.
    *
    * Works with WordPress 1.2+ (4.7+ support added 9-19-2016)
    * Updated 2-27-2017 to use internal WordPress removal for 4.7+ (to prevent PHP warnings output)
    *
    * @param string $tag Filter to remove
    * @param string $class_name Class name for the filter's callback
    @@ -31,6 +32,8 @@ function remove_class_filter( $tag, $class_name = '', $method_name = '', $priori
    * @see https://make.wordpress.org/core/2016/09/08/wp_hook-next-generation-actions-and-filters/
    */
    if ( is_object( $wp_filter[ $tag ] ) && isset( $wp_filter[ $tag ]->callbacks ) ) {
    // Create $fob object from filter tag, to use below
    $fob = $wp_filter[ $tag ];
    $callbacks = &$wp_filter[ $tag ]->callbacks;
    } else {
    $callbacks = &$wp_filter[ $tag ];
    @@ -54,19 +57,24 @@ function remove_class_filter( $tag, $class_name = '', $method_name = '', $priori
    // Method matched, now let's check the Class
    if ( get_class( $filter[ 'function' ][ 0 ] ) === $class_name ) {

    // Now let's remove it from the array
    unset( $callbacks[ $priority ][ $filter_id ] );
    // WordPress 4.7+ use core remove_filter() since we found the class object
    if( isset( $fob ) ){
    // Handles removing filter, reseting callback priority keys mid-iteration, etc.
    $fob->remove_filter( $tag, $filter['function'], $priority );

    // and if it was the only filter in that priority, unset that priority
    if ( empty( $callbacks[ $priority ] ) ) unset( $callbacks[ $priority ] );

    // and if the only filter for that tag, set the tag to an empty array
    if ( empty( $callbacks ) ) $callbacks = array();

    // If using WordPress older than 4.7
    if ( ! is_object( $wp_filter[ $tag ] ) ) {
    } else {
    // Use legacy removal process (pre 4.7)
    unset( $callbacks[ $priority ][ $filter_id ] );
    // and if it was the only filter in that priority, unset that priority
    if ( empty( $callbacks[ $priority ] ) ) {
    unset( $callbacks[ $priority ] );
    }
    // and if the only filter for that tag, set the tag to an empty array
    if ( empty( $callbacks ) ) {
    $callbacks = array();
    }
    // Remove this filter from merged_filters, which specifies if filters have been sorted
    unset( $GLOBALS[ 'merged_filters' ][ $tag ] );
    unset( $GLOBALS['merged_filters'][ $tag ] );
    }

    return TRUE;
  3. @tripflex tripflex revised this gist Sep 20, 2016. No changes.
  4. @tripflex tripflex revised this gist Sep 20, 2016. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion functions.php
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,6 @@
    * @return bool Whether the function is removed.
    */
    function remove_class_filter( $tag, $class_name = '', $method_name = '', $priority = 10 ) {

    global $wp_filter;

    // Check that filter actually exists first
  5. @tripflex tripflex revised this gist Sep 20, 2016. 1 changed file with 2 additions and 8 deletions.
    10 changes: 2 additions & 8 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -7,10 +7,7 @@
    * 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+
    *
    * Supported for WordPress 4.7+ added on September 19, 2016
    *
    * Works with WordPress 1.2+ (4.7+ support added 9-19-2016)
    *
    * @param string $tag Filter to remove
    * @param string $class_name Class name for the filter's callback
    @@ -88,10 +85,7 @@ function remove_class_filter( $tag, $class_name = '', $method_name = '', $priori
    * to a static method. This method allows you to remove actions with a callback to a class
    * you don't have access to.
    *
    * Works with WordPress 1.2+
    *
    * Supported for WordPress 4.7+ added on September 19, 2016
    *
    * Works with WordPress 1.2+ (4.7+ support added 9-19-2016)
    *
    * @param string $tag Action to remove
    * @param string $class_name Class name for the action's callback
  6. @tripflex tripflex revised this gist Sep 20, 2016. 1 changed file with 83 additions and 35 deletions.
    118 changes: 83 additions & 35 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -4,54 +4,102 @@
    *
    * 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.
    * 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+
    *
    * @since 1.0.0
    * Supported for WordPress 4.7+ added on September 19, 2016
    *
    * @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
    * @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 Whether the function is removed.
    */
    function remove_class_filter( $tag, $class_name = '', $method_name = '', $priority = 10 ){
    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 ) {


    // Check that filter actually exists first
    if ( ! isset( $wp_filter[ $tag ] ) ) return FALSE;

    /**
    * If filter config is an object, means we're using WordPress 4.7+ and the config is no longer
    * a simple array, rather it is an object that implements the ArrayAccess interface.
    *
    * To be backwards compatible, we set $callbacks equal to the correct array as a reference (so $wp_filter is updated)
    *
    * @see https://make.wordpress.org/core/2016/09/08/wp_hook-next-generation-actions-and-filters/
    */
    if ( is_object( $wp_filter[ $tag ] ) && isset( $wp_filter[ $tag ]->callbacks ) ) {
    $callbacks = &$wp_filter[ $tag ]->callbacks;
    } else {
    $callbacks = &$wp_filter[ $tag ];
    }

    // Exit if there aren't any callbacks for specified priority
    if ( ! isset( $callbacks[ $priority ] ) || empty( $callbacks[ $priority ] ) ) return FALSE;

    // Loop through each filter for the specified priority, looking for our class & method
    foreach( (array) $callbacks[ $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 ( ! 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 ) {
    if ( ! is_object( $filter[ 'function' ][ 0 ] ) ) continue;

    // Method doesn't match the one we're looking for, goto next
    if ( $filter[ 'function' ][ 1 ] !== $method_name ) continue;

    // Method matched, now let's check the Class
    if ( get_class( $filter[ 'function' ][ 0 ] ) === $class_name ) {

    // Now let's remove it from the array
    unset( $wp_filter[ $tag ][ $priority ][ $filter_id ] );
    unset( $callbacks[ $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 ]);
    if ( empty( $callbacks[ $priority ] ) ) unset( $callbacks[ $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 ]);

    if ( empty( $callbacks ) ) $callbacks = array();

    // If using WordPress older than 4.7
    if ( ! is_object( $wp_filter[ $tag ] ) ) {
    // Remove this filter from merged_filters, which specifies if filters have been sorted
    unset( $GLOBALS[ 'merged_filters' ][ $tag ] );
    }

    return TRUE;
    }
    }

    return FALSE;
    }

    /**
    * Remove Class Action Without Access to Class Object
    *
    * In order to use the core WordPress remove_action() on an action 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 actions with a callback to a class
    * you don't have access to.
    *
    * Works with WordPress 1.2+
    *
    * Supported for WordPress 4.7+ added on September 19, 2016
    *
    *
    * @param string $tag Action to remove
    * @param string $class_name Class name for the action's callback
    * @param string $method_name Method name for the action's callback
    * @param int $priority Priority of the action (default 10)
    *
    * @return bool Whether the function is removed.
    */
    function remove_class_action( $tag, $class_name = '', $method_name = '', $priority = 10 ) {
    remove_class_filter( $tag, $class_name, $method_name, $priority );
    }
  7. @tripflex tripflex created this gist Jun 29, 2016.
    57 changes: 57 additions & 0 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    <?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;
    }