2017-09-27 25 views
2

我以前使用過這裏描述的解決方案:remove_action From PHP Class用於刪除WooCommerce成員資格插件中的操作。remove_action來自WooCommerce會員中的PHP類

但是,該解決方案不再有效,因爲WooComemerce已更改成員資格插件背後的代碼。

所以這是新的代碼。

主營woocommerce-memberships.php

public function includes() { 

    // load post types 
    require_once($this->get_plugin_path() . '/includes/class-wc-memberships-post-types.php'); 

    // load user messages helper 
    require_once($this->get_plugin_path() . '/includes/class-wc-memberships-user-messages.php'); 

    // load helper functions 
    require_once($this->get_plugin_path() . '/includes/functions/wc-memberships-functions.php'); 

    // init general classes 
    $this->rules   = $this->load_class('/includes/class-wc-memberships-rules.php',   'WC_Memberships_Rules'); 
    $this->plans   = $this->load_class('/includes/class-wc-memberships-membership-plans.php', 'WC_Memberships_Membership_Plans'); 
    $this->emails   = $this->load_class('/includes/class-wc-memberships-emails.php',   'WC_Memberships_Emails'); 
    $this->user_memberships = $this->load_class('/includes/class-wc-memberships-user-memberships.php', 'WC_Memberships_User_Memberships'); 
    $this->capabilities  = $this->load_class('/includes/class-wc-memberships-capabilities.php',  'WC_Memberships_Capabilities'); 
    $this->member_discounts = $this->load_class('/includes/class-wc-memberships-member-discounts.php', 'WC_Memberships_Member_Discounts'); 
    $this->restrictions  = $this->load_class('/includes/class-wc-memberships-restrictions.php',  'WC_Memberships_Restrictions'); 

主要實例

function wc_memberships() { 
    return WC_Memberships::instance(); 
} 

從包括類-WC-成員-restrictions.php文件

/** 
* Returns the general content restrictions handler. 
* 
* @since 1.9.0 
* 
* @return null|\WC_Memberships_Posts_Restrictions 
*/ 
public function get_posts_restrictions_instance() { 

    if (! $this->posts_restrictions instanceof WC_Memberships_Posts_Restrictions) { 
     $this->posts_restrictions = wc_memberships()->load_class('/includes/frontend/class-wc-memberships-posts-restrictions.php', 'WC_Memberships_Posts_Restrictions'); 
    } 

    return $this->posts_restrictions; 
} 

然後,在類-WC-成員短柱-restrictions.php

public function __construct() { 

    // decide whether attempting to access restricted content has to be redirected 
    add_action('wp', array($this, 'handle_restriction_modes')); 

    // restrict the post by filtering the post object and replacing the content with a message and maybe excerpt 
    add_action('the_post', array($this, 'restrict_post'), 0); 

如何刪除了 'the_post' 行動?

到目前爲止,我已經在functions.php的主題文件如下:

function weteach_remove_actions(){ 
     if(is_singular('post')) { 
     if(function_exists('wc_memberships')){ 
      remove_action('the_post', array(wc_memberships()->restrictions, 'restrict_post')); 
     } 
     } 
     return; 
    } 
    add_action('the_post', 'weteach_remove_actions', 1); 

,給了我一個「空白頁」誤差。

+0

空白頁面意味着您的網站出現錯誤,請在wp-config中打開WP_DEBUG以查看您遇到的錯誤並首先解決該錯誤。 – davey

+0

嗨,謝謝你的安澤。我剛剛打開了WP_DEBUG,但仍然出現空白屏幕。我也嘗試過「add_action('wp','weteach_remove_actions',1);」而不是og「add_action('the_post','weteach_remove_actions',1);」 –

+0

也嘗試打開'WP_DEBUG_LOG'以將錯誤記錄到'wp-content/debug.log'。雖然我想我發現這段代碼在主要的'woocommerce-memberships.php'文件中缺少'get_restrictions_instance()',所以我沒有正確訪問'post_restrictions'。請檢查我的編輯。 – helgatheviking

回答

1

你能告訴我們錯誤信息是什麼嗎?我的猜測是,restrictionspost_restrictions不是相同的屬性,所以你沒有在正確的類中找到restrict_post方法。

編輯現在,我已經看過成員,這似乎爲我工作:

function so_41431558_remove_membership_post_restrictions(){ 
    if(function_exists('wc_memberships') && version_compare(WC_Memberships::VERSION, '1.9.0', '>=') && is_singular('post')){ 
     remove_action('the_post', array(wc_memberships()->get_restrictions_instance()->get_posts_restrictions_instance(), 'restrict_post'), 0); 
    } 
} 
add_action('wp_head', 'so_41431558_remove_membership_post_restrictions', 1); 

add_action嘗試發生在優先級1,這是後的功能已運行成員方法優先級爲0,所以即使代碼的其餘部分是正確的,也爲時已晚。

所以1.我認爲我們需要去一個更早的鉤子。

和2.我認爲我們需要使用新的方法來訪問郵政限制類實例。

編輯補充

和3.我已經切換到直接版本比較條件

和4.我誤讀其中get_posts_restrictions_instance()方法是......它通過wc_memberships()->get_restrictions_instance()->get_posts_restrictions_instance()

訪問
+0

嗨helgatheviking, 非常感謝你!它的工作原理:-) 但我確實需要添加優先級1到add_action像這樣: add_action('wp_head','so_41431558_remove_membership_post_restrictions',1); 它的工作。 –