Removing added Class Methods from Action/Filters in WordPress

To remove independent function from WP actions/filters is easy thing, we know. We just set remove_action(‘action_name’, ‘function_name’, PRIORITY); and that’s all.

But this simple way doesn’t work for Class Methods. That’s why there is another approach for classes.

remove_action('action_name', array($class_object_variable,'method_name'), PRIORITY);

Well, in some cases this method doesn’t help us either. f.e. When we shouldn’t re-construct that class. (if we declare some $obj=new CLASSNAME(), its construct method re-triggers, and some cases it may cause some problems)

So, what personally i use is guaranteed way – To list already added actions, detect the one we are looking for, get its temporary name generated by WP, and remove it.

In my case i will remove “woocommerce_proceed_to_checkout” action added by third party plugin’s class method “display_form” (priority=9 in my case). Here is how it looks:

add_action('init',function(){
  global $wp_filter;
  if(!empty($wp_filter["woocommerce_proceed_to_checkout"][9])){
    foreach($wp_filter["woocommerce_proceed_to_checkout"][9] as $key=>$removed){
      if(strpos($key,'display_form')!==false){
        remove_action( 'woocommerce_proceed_to_checkout', $key, 9 );
      }
    }
  }
},100);

Discover more from WP DEV - Elvin Haci

Subscribe to get the latest posts sent to your email.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from WP DEV - Elvin Haci

Subscribe now to keep reading and get access to the full archive.

Continue reading