1

I'm trying to use remove_action to prevent a part of a plugin from running - don't ask me why :-).

The function within the plugin is:

add_action( 'woocommerce_before_single_product_summary', array( $this, 'show_product_gallery' ), 30 );

and I'm trying to remove it by:

remove_action( 'woocommerce_before_single_product_summary', array( $this, 'show_product_gallery' ), 30 );

For some reason it isn't doing the trick, although this usually works in Wordpress / WooCommerce.

Can anyone shine a light on why this might be please? I have also tried hooking my function to other things e.g.

add_action( 'init', 'remove_it' );
function remove_it() {
remove_action( 'woocommerce_before_single_product_summary', array( $this, 'show_product_gallery' ), 30 );
}

(Plugin Code: https://codedump.io/share/axGWwMwAH0vn/1/linzs-hook-not-working) Cheers,

Linz

Edited: This question is different to the previous one about remove_action not working, because that was related to the wrong priority - whereas this priority is correct at 30.

Linz Darlington
  • 515
  • 1
  • 10
  • 25
  • I have also tried to use the 'plugins_loaded' hook instead of init, but to no avail. https://codex.wordpress.org/Plugin_API/Action_Reference/plugins_loaded – Linz Darlington Apr 30 '16 at 15:31
  • Have you tried increasing/decreasing the priority of the `remove_action`? Not sure if that will work, but might be worth a shot. – michaelrmcneill Apr 30 '16 at 17:50
  • Possible duplicate of [remove\_action() not working in WordPress plugin](http://stackoverflow.com/questions/10673984/remove-action-not-working-in-wordpress-plugin) – wpclevel May 01 '16 at 11:34
  • Thanks for the spot, Dan. I did actually check out that post before I posted mine, but none of the suggestions worked. I'm using the correct priority (30) and I have tried hooking to to 'plugins_loaded'. Have you got any others ideas? – Linz Darlington May 01 '16 at 13:06
  • Thanks michaelmcneill. I'll give it a go, but as i understand it the remove action needs to share the same priority as the add action, in this case '30'. – Linz Darlington May 01 '16 at 13:07

1 Answers1

1

You need to access the class variable globally. Please try this.

add_action( 'wp', 'remove_it' );
function remove_it() {
 global $WC_Product_Gallery_slider;
 remove_action( 'woocommerce_before_single_product_summary', array( $WC_Product_Gallery_slider, 'show_product_gallery' ), 30 );
}
Joe Beans
  • 294
  • 2
  • 4
  • Pranav! That looks great, thanks and definitely I think it is along the right lines. But it doesn't seem to work - i've tried hooking it to 'plugins_loaded' as well. Any other thoughts? Cheers, Linz – Linz Darlington May 01 '16 at 20:21
  • Consider changing `add_action('init', 'remove_it');` to `add_action( 'wp', 'remove_it', 30 );`. – Joe Beans May 01 '16 at 20:33
  • Ooh, thanks Pranav. That seemed to work! I understand that we've now hooked it to 'wp' not 'init', but would be helpful to know why we added the 30. – Linz Darlington May 02 '16 at 07:05
  • I am also keen to remove this action too: add_action( 'wp', array( $this, 'setup_gallery_archives' ), 20 ); Following your logic is this the thing to go for: add_action( 'wp', 'remove_it2', 20 ); function remove_it2() { global $WC_Product_Gallery_slider; remove_action( 'wp', array( $this, 'setup_gallery_archives' ), 20 ); } – Linz Darlington May 02 '16 at 07:07
  • @LinzDarlington the code `add_action( 'woocommerce_before_single_product_summary', array( $this, 'show_product_gallery' ), 30 );` was in a function which was hooked to `wp`. `remove_action` only works with the correct priority value. It has to be the same as `add_action` – Joe Beans May 02 '16 at 07:51
  • @LinzDarlington for removing `setup_gallery_archives`. Use `add_action( 'wp', 'remove_it2' ); function remove_it2() { global $WC_Product_Gallery_slider; remove_action( 'wp', array( $WC_Product_Gallery_slider, 'setup_gallery_archives' ), 20 ); }` – Joe Beans May 02 '16 at 07:54
  • Pranav - that works excellently. Thank you very much! You are a genius. – Linz Darlington May 02 '16 at 20:21