所以我有这个插件代码
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 }
}
我想修改gridlist_toggle_button函数的内容。 这是我计划修改该函数内容的方式。就像编写另一个函数,几乎与原始函数具有相同的HTML,但带有我的更改。类似于
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 }
}
这样,我就不需要直接更改插件文件了。
为此,我正在尝试删除与其相关的操作。
add_action( 'woocommerce_before_shop_loop', array( $this, 'gridlist_toggle_button' ), 30);
为了使用自己的代码,但我似乎无法移除这个动作。到目前为止,我已经尝试过以下方法。
方法1: 所以我可以使用自己的代码。但是我似乎无法移除这个操作。我到目前为止已经尝试过这个方法。
global $WC_List_Grid ;
remove_action( 'woocommerce_before_shop_loop', array( $WC_List_Grid, 'gridlist_toggle_button' ), 100);
方法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');
方法三
remove_action( 'woocommerce_before_shop_loop', array( 'WC_List_Grid', 'gridlist_toggle_button' ), 100);
似乎它们都没有起作用。
经过一些头脑风暴,我认为可能有两个可能的原因。
- 它不起作用是因为我想要删除的动作并没有直接附加到钩子上。 它是内部动作。
- 我正在尝试通过functions.php阻止gridlist_toggle_button的输出。 但如果插件在functions.php之前加载,则已经调用将被阻止的函数,因此始终会显示输出。
我不擅长面向对象编程。 有人能帮忙吗?
$WP_List_Grid
吗?因为你尝试使用 global 访问它。 - user2917245