WordPress将插件功能替换为自己的插件功能


Wordpress replace plugin function with own one

我在我的WordPress网站中使用woocommerce插件。在插件中有函数widget(),我想调用我自己的函数,所以当网站加载时,而不是插件函数

public function widget($args, $instance)

我的函数将被调用。

我认为它应该用add_filter来完成,因为我找不到如何。

插件文件目录为:

woocommerce/includes/widgets/class-wc-widget-layered-nav.php

请尝试以下代码(将其放在WordPress主题函数中.php)

add_action( 'widgets_init', 'my_widget_replacement', 11 );
function my_widget_replacement() {
    unregister_widget( 'WC_Widget_Layered_Nav' );
    register_widget( 'My_WC_Widget_Layered_Nav' );
}

class My_WC_Widget_Layered_Nav extends WC_Widget_Layered_Nav {
    public function widget( $args, $instance ) {
        // your code
    }
}

这段代码可能不起作用 - 这只是一个想法。