Alex
Alex

Reputation: 105

Remove an action from an external plugin class

I am trying to remove an action from a plugin class WC_GFPA_Entry which has following lines of code:

add_action( 'gform_entry_detail_content_before', array( $this, 'entry_detail_screen_notice' ), 10, 2 );

Here is the Class code:

class WC_GFPA_Entry {

private static $instance;

public static function register() {
    if ( self::$instance == null ) {

        if ( apply_filters( 'woocommerce_gravityforms_create_entries', true ) ) {
            self::$instance = new WC_GFPA_Entry();
        }
    }
}

private $_resuming_orders = array();

private function __construct() {

    add_filter( 'gform_entry_detail_meta_boxes', array( $this, 'add_custom_entry_metabox' ), 10, 3 );

    add_action( 'gform_entry_detail_content_before', array( $this, 'entry_detail_screen_notice' ), 10, 2 );

}

public function add_custom_entry_metabox( $meta_boxes, $entry, $form ) {
    // some code 
}

public function entry_detail_screen_notice( $form, $lead ) {
    // some code .....
}
}

What I have tried in functions.php so far:

remove_action( 'gform_entry_detail_content_before', array( WC_GFPA_Entry::register(), 'entry_detail_screen_notice' ), 10, 2 );

And:

function remove_action(){
    remove_action( 'gform_entry_detail_content_before', array( WC_GFPA_Entry::register(), 'entry_detail_screen_notice' ), 10, 2 );
}
add_action('init', 'remove_action');

I have also tried changing priority etc but nothing really worked. Any help would be appreciated.

Upvotes: 0

Views: 1196

Answers (1)

Ionuț Staicu
Ionuț Staicu

Reputation: 22164

Unfortunately, there is no easy way of doing this. However, it is doable by using an utility like this:

https://github.com/herewithme/wp-filters-extras/blob/master/wp-filters-extras.php

So in your example that would be:

remove_filters_for_anonymous_class('gform_entry_detail_content_before', 'WC_GFPA_Entry', 'entry_detail_screen_notice', 10)

Lib code above:

function remove_filters_with_method_name( $hook_name = '', $method_name = '', $priority = 0 ) {
    global $wp_filter;
    // Take only filters on right hook name and priority
    if ( ! isset( $wp_filter[ $hook_name ][ $priority ] ) || ! is_array( $wp_filter[ $hook_name ][ $priority ] ) ) {
        return false;
    }
    // Loop on filters registered
    foreach ( (array) $wp_filter[ $hook_name ][ $priority ] as $unique_id => $filter_array ) {
        // Test if filter is an array ! (always for class/method)
        if ( isset( $filter_array['function'] ) && is_array( $filter_array['function'] ) ) {
            // Test if object is a class and method is equal to param !
            if ( is_object( $filter_array['function'][0] ) && get_class( $filter_array['function'][0] ) && $filter_array['function'][1] == $method_name ) {
                // Test for WordPress >= 4.7 WP_Hook class (https://make.wordpress.org/core/2016/09/08/wp_hook-next-generation-actions-and-filters/)
                if ( is_a( $wp_filter[ $hook_name ], 'WP_Hook' ) ) {
                    unset( $wp_filter[ $hook_name ]->callbacks[ $priority ][ $unique_id ] );
                } else {
                    unset( $wp_filter[ $hook_name ][ $priority ][ $unique_id ] );
                }
            }
        }
    }
    return false;
}
/**
 * Allow to remove method for an hook when, it's a class method used and class don't have variable, but you know the class name :)
 */
function remove_filters_for_anonymous_class( $hook_name = '', $class_name = '', $method_name = '', $priority = 0 ) {
    global $wp_filter;
    // Take only filters on right hook name and priority
    if ( ! isset( $wp_filter[ $hook_name ][ $priority ] ) || ! is_array( $wp_filter[ $hook_name ][ $priority ] ) ) {
        return false;
    }
    // Loop on filters registered
    foreach ( (array) $wp_filter[ $hook_name ][ $priority ] as $unique_id => $filter_array ) {
        // Test if filter is an array ! (always for class/method)
        if ( isset( $filter_array['function'] ) && is_array( $filter_array['function'] ) ) {
            // Test if object is a class, class and method is equal to param !
            if ( is_object( $filter_array['function'][0] ) && get_class( $filter_array['function'][0] ) && get_class( $filter_array['function'][0] ) == $class_name && $filter_array['function'][1] == $method_name ) {
                // Test for WordPress >= 4.7 WP_Hook class (https://make.wordpress.org/core/2016/09/08/wp_hook-next-generation-actions-and-filters/)
                if ( is_a( $wp_filter[ $hook_name ], 'WP_Hook' ) ) {
                    unset( $wp_filter[ $hook_name ]->callbacks[ $priority ][ $unique_id ] );
                } else {
                    unset( $wp_filter[ $hook_name ][ $priority ][ $unique_id ] );
                }
            }
        }
    }
    return false;
}

Upvotes: 1

Related Questions