删除插件操作时出现问题

时间:2018-03-08 作者:radscheit

在我的functions.php 我尝试从插件类中删除一个操作。请参见下面的两个代码。删除不起作用,但当我注释掉add_action (第39行)在plugin类中,则它可以工作。我做错了什么?

function remove_trusted_theme_for_wholesale_customers() {
remove_action(\'woocommerce_thankyou\', 
array(\'WC_GZD_Trusted_Shops_Template_Hooks\',\'template_thankyou\'), 10);
};
add_action( \'template_redirect\', \'remove_trusted_theme_for_wholesale_customers\' );
以下是插件:

<?php

class WC_GZD_Trusted_Shops_Template_Hooks {

    protected static $_instance = null;

    public $base = null;

    public static function instance( $base ) {
        if ( is_null( self::$_instance ) )
            self::$_instance = new self( $base );
        return self::$_instance;
    }

    private function __construct( $base ) {

        $this->base = $base;

        // Template actions
        if ( $this->base->is_enabled() )
            add_action( \'after_setup_theme\', array( $this, \'template_hooks\' ), 13 );

        if ( $this->base->is_product_reviews_enabled() ) {
            add_filter( \'woocommerce_product_tabs\', array( $this, \'remove_review_tab\' ), 40, 1 );
        }

        if ( $this->base->is_product_sticker_enabled() ) {
            add_filter( \'woocommerce_product_tabs\', array( $this, \'review_tab\' ), 50, 1 );
        }

        if ( $this->base->is_product_widget_enabled() ) {
            add_filter( \'woocommerce_gzd_template_name\', array( $this, \'set_product_widget_template\' ), 50, 1 );
        }

    }

    public function template_hooks() {

        add_action( \'woocommerce_thankyou\', array( $this, \'template_thankyou\' ), 10, 1 );
        add_action( \'wp_footer\', array( $this, \'template_trustbadge\' ), PHP_INT_MAX );

    }

    public function set_product_widget_template( $template ) {

        if ( in_array( $template, array( \'single-product/rating.php\' ) ) )
            $template = \'trusted-shops/product-widget.php\';

        return $template;

    }

    public function remove_review_tab( $tabs ) {

        if ( isset( $tabs[ \'reviews\' ] ) )
            unset( $tabs[ \'reviews\' ] );
        return $tabs;

    }

    public function review_tab( $tabs ) {
        $tabs[ \'trusted_shops_reviews\' ] = array(
            \'title\' => _x( \'Reviews\', \'trusted-shops\', \'woocommerce-germanized\' ),
            \'priority\' => 30,
            \'callback\' => array( $this, \'template_product_sticker\' ),
        );
        return $tabs;
    }

    public function template_product_sticker( $template ) {
        wc_get_template( \'trusted-shops/product-sticker.php\' );
    }

    public function template_trustbadge() {
        wc_get_template( \'trusted-shops/trustbadge.php\' );
    }

    public function template_thankyou( $order_id ) {
        wc_get_template( \'trusted-shops/thankyou.php\', array(
            \'order_id\' => $order_id,
            \'gtin_attribute\' => $this->base->gtin_attribute,
            \'brand_attribute\' => $this->base->brand_attribute,
            \'mpn_attribute\' => $this->base->mpn_attribute,
        ) );
    }

}

1 个回复
最合适的回答,由SO网友:josephmcm 整理而成

您所做的只是删除类的静态方法中添加的操作。

从…起https://codex.wordpress.org/Function_Reference/remove_action

如果某个动作是从类中添加的,例如通过插件添加的,则删除该动作将需要通过保存类实例的变量访问该类。

add_action( \'wp_head\', \'remove_my_class_action\' );
function remove_my_class_action(){
  global $my_class;
  remove_action( \'wp_footer\', array( $my_class, \'class_function_being_removed\' ) );
}

结束