Remove Action from Plugin within extended class and no assigned variable

I have tried so many solutions for this but I can't figure it out. Here is a simplified version of the plugin code:

class YITH_Vendors_Frontend_Premium extends YITH_Vendors_Frontend {
    public function __construct() {

    add_action( 'woocommerce_register_form', array( $this, 'register_form' ) );
}

So I want to remove this action from my child themes function.php.

The problem is the class is not instanced via a variable. Instead it is instanced like this:

class YITH_Vendors_Premium extends YITH_Vendors {
public function __construct() {
public function init() {
            $this-frontend = new YITH_Vendors_Frontend_Premium();
    }
  }
}

This class however is never being instanced via variable. Instead there is a function that instances it:

function YITH_Vendors() {

    if ( defined( 'YITH_WPV_PREMIUM' ) ) {
        return YITH_Vendors_Premium::instance();
    }

    return YITH_Vendors::instance();
  }

And then it's just called like that:

YITH_Vendors();

I tried all these but no chance:

remove_action( 'woocommerce_register_form', array( "YITH_Vendors_Frontend", 'register_form' ), 999 );
remove_action( 'woocommerce_register_form', array( "YITH_Vendors_Frontend_Premium", 'register_form' ), 999 );
remove_action( 'woocommerce_register_form', array( "YITH_Vendors", 'register_form' ), 999 );
remove_action( 'woocommerce_register_form', array( "YITH_Vendors_Premium", 'register_form' ), 999 );

Please help! Thanks!

Topic deactivation actions Wordpress

Category Web


You'll want to find the right action to call this. After the action has been added, but before it is called. Then get the object's instance of the callback and the same priority (10 is default).

remove_action('woocommerce_register_form', array ( YITH_Vendors_Premium::instance()->frontend, 'register_form' ), 10 );

It will not work using a simple remove_action, you need a custom function to replace as anonymous object. Also make sure it trigger after the 'woocommerce_product_query' filter has been set.

Try this function:

function remove_anonymous_object_filter( $tag, $class, $method ) 
                {
                    $filters = false;

                    if ( isset( $GLOBALS['wp_filter'][$tag] ) )
                        $filters = $GLOBALS['wp_filter'][$tag];

                    if ( $filters )
                    foreach ( $filters as $priority => $filter ) 
                        {
                            foreach ( $filter as $identifier => $function ) 
                                {
                                    if ( ! is_array( $function ) )
                                        continue;

                                    if ( ! $function['function'][0] instanceof $class )
                                        continue;

                                    if ( $method == $function['function'][1] ) 
                                        {
                                            remove_filter($tag, array( $function['function'][0], $method ), $priority);
                                        }
                                }
                        }
                }

For your particular filter you should try this:

remove_anonymous_object_filter('woocommerce_product_query',             'YITH_Vendors_Frontend_Premium', 'hide_vendors_product');

You should also check if the anonymous object filter exists, use this functio:

function anonymous_object_filter_exists($tag, $class, $method)
                {
                    if ( !  isset( $GLOBALS['wp_filter'][$tag] ) )
                        return FALSE;

                    $filters = $GLOBALS['wp_filter'][$tag];

                    if ( !  $filters )
                        return FALSE;

                    foreach ( $filters as $priority => $filter ) 
                        {
                            foreach ( $filter as $identifier => $function ) 
                                {
                                    if ( ! is_array( $function ) )
                                        continue;

                                    if ( ! $function['function'][0] instanceof $class )
                                        continue;

                                    if ( $method == $function['function'][1] ) 
                                        {
                                            return TRUE;
                                        }
                                }
                        }

                    return FALSE;
                }

Then use this:

if (anonymous_object_filter_exists('woocommerce_product_query',             'YITH_Vendors_Frontend_Premium', 'hide_vendors_product'))
    { .. }

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.