remove_action() not working in WordPress Theme

One of my plugin have this coding,

Class Afke{
public function __construct() {
    add_action( 'init', array( $this, 'setup' ), -1 );
}

function setup() {
    add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
}
}

I tried by removing ‘add_meta_boxes’ in theme functions.php by adding the below code,

Read More
function remove_thematic_actions(){
 $bcatcf = new Afke();
remove_action( 'add_meta_boxes', array( $bcatcf, 'add_meta_boxes' ));
//remove_action( 'add_meta_boxes', 'add_meta_boxes' );
}

add_action('init','remove_thematic_actions');

But not able to remove this action. Kindly suggest me.

Related posts

Leave a Reply

2 comments

  1. You have used ;

    add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
    

    in Afke class, but there is no such a function add_meta_boxes. Use your function like this;

    Class Afke{
      public function __construct() {
          add_action( 'init', array( $this, 'setup' ), -1 );
      }
    
      function setup() {
          add_action( 'add_meta_boxes', array( $this, 'my_meta_box_function' ) );
      }
    
      function my_meta_box_function() {
        // do your things here
      }
    }
    

    And, in order to remove that action you need to use;

    function remove_thematic_actions(){
      $bcatcf = new Afke();
      remove_action( 'add_meta_boxes', array( $bcatcf, 'my_meta_box_function' ));
    }
    
    add_action('init','remove_thematic_actions');
    
  2. Instead of creating a new instance, assigning the original to a variable allows to target it:

    Class Afke2{
        public function __construct() {
            add_action( 'init', array( $this, 'setup' ), -1 );
        }
    
        function setup() {
            add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
        }
    
        function add_meta_boxes()
        {
            add_meta_box(
                'test_metabox',
                __( 'Test meta box' ),
                function() { echo 'Hello world!'; },
                'post'
            );
        }
    }
    $myAfke = new Afke2;
    

    Then, elsewhere, in another plugin or in the theme’s functions.php:

    function remove_thematic_actions(){
        global $myAfke;
        remove_action( 'add_meta_boxes', array( $myAfke, 'add_meta_boxes' ));
    }
    add_action('init','remove_thematic_actions');