【问题标题】:How to Unhook actions/filters in within Class in plugin如何在插件的类中取消挂钩操作/过滤器
【发布时间】:2019-11-10 20:52:48
【问题描述】:

我有一个出租度假屋的商业网站,其中 WordPress 主题有自己的预订系统,仅用于预订房屋,非常棒!

我也可以预订旅游/服务(不是房屋),但对于这些我使用 WooCommerce/WooCommerce 预订插件,而不是房屋预订系统。

一切都很棒!

但最近主题更新为家庭预订系统引入了 WooCommerce 结帐选项,该选项在更新之前从未存在过。虽然我关闭了主题 WooCommerce 结帐选项,但代码仍在主题的核心插件中执行。几周来,我一直在与主题提供商提供开放支持票证,以便他们在用户关闭选项时放置条件语句以不运行新代码,但我不知道何时或是否它将永远完成。现在发生的事情是,主题的核心插件正在操纵 WooCommerce 结帐,并且在下订单的最后一步之后,我以前一直单独使用 WooCommerce 的任何旅游/服务现在在下订单后超时并产生超过 1,000 个额外后端的“幻影”结帐页面。

底线: 当从主题的核心插件文件中注释掉 3 行(第 27-29 行)时,一切正常。但我不想在每次插件更新后都注释掉这 3 行。

//add_action( 'woocommerce_thankyou', array($this,'order_attach') );

//add_filter( 'woocommerce_checkout_fields', array($this,'custom_override_checkout_fields') );

//add_filter('woocommerce_create_account_default_checked', '__return_true');

这里是完整的代码(第 27-29 行被注释掉了):https://pastebin.com/jqtBpCpA

我已尝试以下两个页面上的说明,但均未成功: https://mekshq.com/remove-wordpress-action-filter-class https://github.com/herewithme/wp-filters-extras

我在functions.php文件中插入了以下内容

方法一

global $Wpestate_Global_Payments; //get access to the class object instance

remove_action( 'woocommerce_thankyou', array($Wpestate_Global_Payments,'order_attach') );
remove_filter( 'woocommerce_checkout_fields', array($Wpestate_Global_Payments,'custom_override_checkout_fields') ); 
remove_filter('woocommerce_create_account_default_checked', '__return_true');

然后使用插件,这里:https://github.com/herewithme/wp-filters-extras 两种方法我都试过了:

方法 2:

remove_filters_with_method_name( 'woocommerce_thankyou', 'order_attach' );
remove_filters_with_method_name( 'woocommerce_checkout_fields', 'custom_override_checkout_fields' );
remove_filters_with_method_name( 'woocommerce_create_account_default_checked', '__return_true' );

方法 3:

remove_filters_for_anonymous_class( 'woocommerce_thankyou', 'Wpestate_Global_Payments', 'order_attach' );
remove_filters_for_anonymous_class( 'woocommerce_checkout_fields', 'Wpestate_Global_Payments', 'custom_override_checkout_fields' );
remove_filters_for_anonymous_class( 'woocommerce_create_account_default_checked', 'Wpestate_Global_Payments', '__return_true' );
class Wpestate_Global_Payments {

    public $stripe_payments;
    public $is_woo;  
    public $userID;
    public $user_email;

    function __construct() {

        $this->is_woo   =   wprentals_get_option('wp_estate_enable_woo','') ;
        $current_user   =   wp_get_current_user();

        $this->userID                  =    $current_user->ID;
        $this->user_email              =    $current_user->user_email;
        add_filter( 'woocommerce_cart_item_permalink','__return_false');
        add_action( 'wp_ajax_wpestate_woo_pay',                 array( $this, 'wpestate_woo_pay') );
        add_action( 'wp_ajax_mopriv_wpestate_woo_pay',          array( $this, 'wpestate_woo_pay') );
        add_filter( 'woocommerce_thankyou_order_received_text', array($this, 'wpestate_woocommerce_thankyou_order_received_text'),10,2 );
        add_action( 'woocommerce_before_single_product',        array($this, 'wpestate_product_redirect') );
        add_action( 'woocommerce_product_query',                array($this, 'wpestate_custom_pre_get_posts_query' ));  
        add_action( 'woocommerce_order_status_completed',       array($this, 'wpestate_payment_complete') );
        add_action( 'woocommerce_order_status_processing',      array($this, 'wpestate_payment_complete') );       
       //EVERYTHING WORKS ONLY WHEN THE 3 LINES BELOW ARE COMMENTED OUT 
       //add_action( 'woocommerce_thankyou', array($this,'order_attach') );
       //add_filter( 'woocommerce_checkout_fields', array($this,'custom_override_checkout_fields') );
       //add_filter('woocommerce_create_account_default_checked', '__return_true');

任何人都可以提供有关如何在functions.php 中为每个操作插入remove_action 和remove_filter 以删除/取消挂钩我目前在插件文件中注释掉的3 个操作/过滤器的任何见解吗?

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    我不能 100% 确定,但我的猜测是,在 Wpestate_Global_Payments 类之前调用​​了您移除钩子的尝试。这基本上意味着它们不能被删除,因为它们还没有被添加。

    我建议重试您的方法,但当wp_loaded 操作被触发时。

    wp_loaded 一旦 WP、所有插件和主题完全加载并实例化,就会触发此钩子。

    例如,在您的functions.php 文件中...

    function remove_my_themes_actions() {
      remove_filters_with_method_name( 'woocommerce_thankyou', 'order_attach' );
      remove_filters_with_method_name( 'woocommerce_checkout_fields', 'custom_override_checkout_fields' );
      remove_filters_with_method_name( 'woocommerce_create_account_default_checked', '__return_true' );
    }
    
    add_action( 'wp_loaded', 'remove_my_themes_actions' );
    

    如果您运行的 PHP 版本为 5.3.0 或更高版本,您可以使用 anonymous function...

    add_action( 'wp_loaded', function() {
      remove_filters_with_method_name( 'woocommerce_thankyou', 'order_attach' );
      remove_filters_with_method_name( 'woocommerce_checkout_fields', 'custom_override_checkout_fields' );
      remove_filters_with_method_name( 'woocommerce_create_account_default_checked', '__return_true' );
    } );
    

    希望这会有所帮助!

    【讨论】:

    • 谢谢@levi-cole 我对删除过滤器/操作时有同样的感觉。我尝试了您的两个建议以及您的相同理论,但我将其应用于我的方法 1,但不幸的是,这 3 次尝试都没有奏效。 add_action( 'wp_loaded', function() { remove_action( 'woocommerce_thankyou', array(Wpestate_Global_Payments,'order_attach') ); remove_filter( 'woocommerce_checkout_fields', array(Wpestate_Global_Payments,'custom_override_checkout_fields') ); remove_filter('woocommerce_create_account_default_checked', '__return_true'); } );
    • 我也在使用 PHP 7.3。
    猜你喜欢
    • 2020-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-28
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多