0
votes

I am trying to replace or overwrite an action hook declared inside a wordpress plugin class

class WCFM_Withdrawal {

    public function __construct() {
        global $WCFM;
            
            // WCFMu Thirdparty Ajax Controller
            add_action( 'after_wcfm_ajax_controller', array( &$this, 'wcfm_withdrawal_ajax_controller' ) );
            

        
    } 

Here the add_action hook which points the function. I want to replace "wcfm_withdrawal_ajax_controller" with my own function.

add_action("init", function () {
    // removing the woocommerce hook
  global $WCFM;
    remove_action( 'after_wcfm_ajax_controller', array( $WCFM, 'wcfm_withdrawal_ajax_controller' ) );
});

add_action( 'after_wcfm_ajax_controller', 'my_new_function'  );

function my_new_function(){

  //Do something here
}

i have tried to remove_action and add my own function, but didn't work.

1

1 Answers

0
votes

Make sure you have the correct Class instance. Your example shows $WCFM, but the class declaration is WCFM_Withdrawal. Is $WCFM an instance of WCFM_Withdrawal? If you have determined that you do have the appropriate class instance, you may be running the remove_action too early (before the one you want to remove is added).

You can try lowering the priority of your init function:

add_action( 'init', function () {
    // removing the woocommerce hook
    global $WCFM;
    remove_action( 'after_wcfm_ajax_controller', array( $WCFM, 'wcfm_withdrawal_ajax_controller' ) );
}, 99 );

I'd say you could also try a later Action Hook than init, but since this appears to be an AJAX handler, that probably won't suffice. Start by making sure that you've got the correct class instance variable, and then bump down the priority of your removal.