need help removing action from plugin file

前端 未结 6 1598
醉话见心
醉话见心 2021-01-22 03:10

Hello I am trying to remove an action from a wordpress plugin file. The plugin is called Woocommerce Points and Rewards. I have found the action I want to remove in one of the

6条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-22 03:59

    SOLUTION-1: In this case, as we have the plugin object's global instance, then we can do it easily:

    remove_action('woocommerce_before_cart',array($GLOBALS['wc_points_rewards']->cart,'render_redeem_points_message'),16);
    

    SOLUTION-2: We will not be lucky always like the above solution getting the instance of the class object if any plugin author creates the class object anonymously without storing it to any global variables or keeping no method which can return it's own instance. In those cases, we can use the following :)

    //keeping this function in our functions.php
    function remove_anonymous_object_action( $tag, $class, $method, $priority=null ){
    
        if( empty($GLOBALS['wp_filter'][ $tag ]) ){
            return;
        }
    
        foreach ( $GLOBALS['wp_filter'][ $tag ] as $filterPriority => $filter ){
            if( !($priority===null || $priority==$filterPriority) )
                continue;
    
            foreach ( $filter as $identifier => $function ){
                if( is_array( $function)
                    and is_a( $function['function'][0], $class )
                    and $method === $function['function'][1]
                ){
                    remove_action(
                        $tag,
                        array ( $function['function'][0], $method ),
                        $filterPriority
                    );
                }
            }
        }
    }
    

    And calling the following line appropriately when we need (may be with a hook or something):

    //-->Actual Target: this line does not work;
    //remove_action( 'personal_options', array('myCRED_Admin','show_my_balance') );
    
    //-->But instead this line will work ;)
    remove_anonymous_object_action('personal_options','myCRED_Admin','show_my_balance');
    

提交回复
热议问题