i have class like this

if ( ! class_exists( 'My_class' ) ) {
    class My_class {
        public function __construct() {
            add_action( 'woocommerce_shop_loop_item_title', array( $this, 'change_title' ), 10 );

        public function change_title() {
            echo 'The title';

return new My_class();

I trying to remove_action change_title by using

remove_action( 'woocommerce_before_shop_loop_item_title', array( 'My_class', 'change_title' ), 10 );
// or
remove_action( 'woocommerce_before_shop_loop_item_title', 10 );

But it does not work because I don't really understand PHP OOP. How can I do that, is it possible?

And can i remove it via a plugin?



1 Answer 1


It's not possible to remove it with remove_action() the way you've written it.

When you hooked it here:

add_action( 'woocommerce_shop_loop_item_title', array( $this, 'change_title' ), 10 );

The $this means that the function that's hooked is on this specific instance of My_class. When using remove_action() you need to pass the same instance of the class:

remove_action( 'woocommerce_before_shop_loop_item_title', array( $instance, 'change_title' ), 10 );

(where $instance is the instance of the class).

The problem is that the instance of the class is not available anywhere else because you've instantiated into nothing:

return new My_class();

To remove the action you need to put the class instance into a variable and then use that variable to remove it later:

$my_class = new My_class();

Then in your other code:

global $my_class;

remove_action( 'woocommerce_before_shop_loop_item_title', array( $my_class, 'change_title' ), 10 );

Another option is that if change_title is static then you don't need a specific instance of the class to add and remove it from hooks.

So make the function static:

public static function change_title() {
    echo 'The title';

Then to hook a static method you do this:

add_action( 'woocommerce_shop_loop_item_title', array( 'My_class', 'change_title' ), 10 );

Which means you can remove it like this:

remove_action( 'woocommerce_before_shop_loop_item_title', array( 'My_class', 'change_title' ), 10 );

But whether your class/functions can or should be static is a larger architectural question that would depend on exactly what you're doing.

  • Thanks, it's works on my theme. Another question: can i remove it via a plugin? The same way can work? Sorry, I haven't worked with the plugin ever
    – ttn_
    Aug 17, 2018 at 7:38
  • 1
    @ttn_ Yes. It doesn't matter if it was added by theme or plugin. You just need to care that you remove it after it has been added (and not before, so look out for order of execution)
    – kero
    Aug 17, 2018 at 8:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.