Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Big Brother eviction of @tarendai's Davina swear.
<?php
// Solution for https://github.com/Tarendai/davina
/**
* Remove filter or action method added from an unassigned object.
*
* Solves the immediate case. Not bothered to generalise it.
*
* @global array $wp_filter Storage for all of the filters and actions.
*
* @param string $tag The filter hook to which the function to be removed is hooked.
* @param callback $function_to_remove The class and method name which should be removed.
* @param int $priority Optional. The priority of the function (default: 10).
*/
function bigbrother_remove_filter( $tag, $function_to_add, $priority ) {
global $wp_filter;
foreach( $wp_filter[ $tag ][ $priority ] as $index => $filter ) {
if (
$function_to_add[0] == get_class( $filter['function'][0] ) && // class
$function_to_add[1] == $filter['function'][1] // method
) {
unset( $wp_filter[ $tag ][ $priority ][$index] );
}
}
}
// This is Big Brother. Davina, you have been evicted.
bigbrother_remove_filter( 'the_content', array( 'Davina_Plugin', 'swear' ), -1 );
@bueltge
Copy link

bueltge commented Oct 4, 2014

I'm also interest on an clean solution. Below my test. On get_class it is important, that you check for object. @Rarst is my additional right inside this closure?

<?php
// Change the content for test
add_filter( 'the_content', 'bigbrother_test', -1 );
function bigbrother_test() {

    return 'TEST';
}

/**
 * Remove filter or action method added from an unassigned object.
 *
 * Solves the immediate case. Not bothered to generalise it.
 *
 * @global array $wp_filter Storage for all of the filters and actions.
 *
 * @param string   $tag                The filter hook to which the function to be removed is hooked.
 * @param callback $function_to_remove The class and method name which should be removed.
 * @param int      $priority           Optional. The priority of the function (default: 10).
 */
function bigbrother_remove_filter( $tag, $function_to_add, $priority ) {
    global $wp_filter;

    foreach( $wp_filter[ $tag ][ $priority ] as $index => $filter ) {

        if (
            is_object( $filter['function'][0] ) &&
            $function_to_add[0] == get_class( $filter['function'][0] ) && // class
            $function_to_add[1] == $filter['function'][1]    // method
        ) {
            unset( $wp_filter[ $tag ][ $priority ][$index] );
        }

    }
}


function functional_bigbrother_remove_filter( $tag, $function_to_remove, $priority ) {
    global $wp_filter;

    $wp_filter[ $tag ][ $priority ] = array_filter(

        $wp_filter[ $tag ][ $priority ],

        function ( $item ) use ( $function_to_remove ) {

            if ( is_object( $item['function'][0] ) )
                return [ 
                    get_class( $item['function'][0] ),
                    $item['function'][1]
                ] !== $function_to_remove;
        }
    );
}

// This is Big Brother. Davina, you have been evicted.
functional_bigbrother_remove_filter( 'the_content', 'bigbrother_test', -1 );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment