WordPress - remove an action defined in a plugin class

So I have this plugin code

class WC_List_Grid { public function __construct() { add_action( 'wp' , array( $this, 'setup_gridlist' ) , 20); } function setup_gridlist() { add_action( 'woocommerce_before_shop_loop', array( $this, 'gridlist_toggle_button' ), 30); } function gridlist_toggle_button() {?> <nav class="gridlist-toggle"> <a href="#" id="grid" title="<?php _e('Grid view', 'woocommerce-grid-list-toggle'); ?>"><span class="dashicons dashicons-grid-view"></span> <em><?php _e( 'Grid view', 'woocommerce-grid-list-toggle' ); ?></em></a> <a href="#" id="list" title="<?php _e('List view', 'woocommerce-grid-list-toggle'); ?>"><span class="dashicons dashicons-exerpt-view"></span> <em><?php _e( 'List view', 'woocommerce-grid-list-toggle' ); ?></em></a> </nav> <?php } } 

I want to change the contents of the gridlist_toggle_button function. This is how I plan to change the contents of this function. Like writing another function with almost the same html as the original, but a bit of my changes. Sort of

 add_action('woocommerce_before_shop_loop','new_gridlist_toggle_button') function new_gridlist_toggle_button() {?> <nav class="gridlist-toggle"> <a href="#" class="grid-view" title="<?php _e('Grid view', 'woocommerce-grid-list-toggle'); ?>"><span class="dashicons dashicons-grid-view"></span> <em><?php _e( 'Grid view', 'woocommerce-grid-list-toggle' ); ?></em></a> <a href="#" class="list-view" title="<?php _e('List view', 'woocommerce-grid-list-toggle'); ?>"><span class="dashicons dashicons-exerpt-view"></span> <em><?php _e( 'List view', 'woocommerce-grid-list-toggle' ); ?></em></a> </nav> <?php } } 

This way, I donโ€™t have to directly change the plugin files

For this, I am trying to remove a related action.

 add_action( 'woocommerce_before_shop_loop', array( $this, 'gridlist_toggle_button' ), 30); 

So that I can use my own code. But I can not delete this action. I have tried this so far.

Method 1:

 global $WC_List_Grid ; remove_action( 'woocommerce_before_shop_loop', array( $WC_List_Grid, 'gridlist_toggle_button' ), 100); 

Method 2:

 function remove_plugin_actions(){ global $WC_List_Grid ; remove_action( 'woocommerce_before_shop_loop', array( $WC_List_Grid, 'gridlist_toggle_button' ), 100); } add_action('init','remove_plugin_actions'); 

Method 3

 remove_action( 'woocommerce_before_shop_loop', array( 'WC_List_Grid', 'gridlist_toggle_button' ), 100); 

None of them seem to work.

With a little brainstorming, I think there may be two possible reasons.

  • It does not work, because the action I want to delete is not directly attached to the hook. Its action is within action.
  • I am trying to block the output of gridlist_toggle_button via functions.php. But if the plugins are loaded before functions.php, then the function that should be blocked is already called and, therefore, it always shows the output.

I am not very good at OOP. Can someone please help me?

+5
source share
1 answer

Whenever $wc_lg = new WC_List_Grid() is created, it starts wp . Immediately after creating a global object of the WP class, the $wg_lg will call setup_gridlist() on its own. Its trigger trigger woocommerce_before_shop_loop . And whenever this happens, again $wg_lg will call the function on itself. Namely gridlist_toggle_button() .

I want to change the contents of the gridlist_toggle_button function

Why not just change it inside the plugin? How to redefine everything inside.

global $ WC_List_Grid;

How do you know that $WC_List_Grid has its own name? This is just the name of the class. It can be created under any name, for example $foo or $bar .

I assume that you are encoding the functions.php file of your template. Plugins are loaded before functions. Php and remove_action() can only work after add_action() happend.

Important: To remove a hook, $ function_to_remove and $ priority arguments must match when the hook was added. This applies to both filters and actions. A refusal warning will not be issued.

http://codex.wordpress.org/Function_Reference/remove_action

So, this really should only work to remove the action, but how will this help you change the contents inside the function?

 remove_action( 'woocommerce_before_shop_loop', array('WC_List_Grid', 'gridlist_toggle_button'), 30 ); 

Please note that you have used priority 100

EDIT

I found out that do_action('woocommerce_archive_description') happens in archive-product.php and in wc-template-functions.php before woocommerce_before_shop_loop hook actions are performed in the plugin. Try using this:

 function remove_plugin_actions(){ global $WC_List_Grid; remove_action( 'woocommerce_before_shop_loop', array( $WC_List_Grid, 'gridlist_toggle_button' ), 30); } add_action('woocommerce_archive_description','remove_plugin_actions'); 
+3
source

Source: https://habr.com/ru/post/1247172/


All Articles