【问题标题】:remove_action From PHP Class in WooCommerce Memberships从 WooCommerce 会员资格中的 PHP 类中删除操作
【发布时间】:2017-09-27 12:43:19
【问题描述】:

我之前使用过这里描述的解决方案:remove_action From PHP Class 用于删除 WooCommerce 会员插件中的操作。

但是,该解决方案不再有效,因为 WooComerce 更改了会员插件背后的代码。

所以这是新代码。

主要的 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();
}

来自包含的 class-wc-memberships-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;
}

然后在class-wc-memberships-posts-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 );

这给了我一个“空白页”错误。

【问题讨论】:

  • 空白页表示您的网站有错误,请在 wp-config 中打开 WP_DEBUG 以查看您遇到的错误并首先修复它。
  • 您好,感谢您的回答。我刚刚打开了 WP_DEBUG,但仍然是一个空白屏幕。我也试过“add_action('wp', 'weteach_remove_actions', 1);”代替 og "add_action('the_post', 'weteach_remove_actions', 1 );"
  • 同时尝试开启WP_DEBUG_LOG 将错误记录到wp-content/debug.log。虽然我想我发现这段代码在woocommerce-memberships.php 主文件中缺少get_restrictions_instance(),所以我没有正确访问post_restrictions。请检查我的编辑。

标签: php wordpress woocommerce hook-woocommerce woocommerce-memberships


【解决方案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 上运行 Memberships 方法,所以即使您的其余代码是正确的,它也是正确的晚的。

所以 1. 我认为我们需要进行更早的钩子。

还有 2. 我认为我们需要使用新方法来访问 post 限制类实例。

编辑添加

和 3. 我已经切换到直接版本比较条件

和 4. 我误读了 get_posts_restrictions_instance() 方法的位置......它是通过 wc_memberships()->get_restrictions_instance()->get_posts_restrictions_instance() 访问的

【讨论】:

  • 嗨 helgatheviking,非常感谢!它可以工作:-) 但是我确实需要像这样将优先级 1 添加到 add_action 中: add_action( 'wp_head', 'so_41431558_remove_membership_post_restrictions', 1 );让它工作。
猜你喜欢
  • 2017-10-24
  • 2018-12-20
  • 2019-08-29
  • 1970-01-01
  • 2022-08-22
  • 1970-01-01
  • 2015-01-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多