php - WooCommerce 成员(member)资格 : Conditional to check a page access

标签 php wordpress woocommerce conditional-statements woocommerce-memberships

我有一个基于 WooCommerce 构建的 Wordpress 成员(member)网站,并使用 WooCommerce 成员(member)插件来限制某些页面仅限成员(member)访问。

其中一些页面是“滴灌”的......即。购买后 3 天即可访问这些页面,等等。我已在 WooMemberships 中进行了设置。

我试图简单地进行 PHP 条件检查以查看当前用户是否有权访问某个页面。

我在文档中找到了这段代码:wc_memberships_is_post_content_restricted()

但是,我一直无法让它发挥作用。

是否有一个代码片段基本上可以执行 PHP IF 语句来判断当前用户是否有权访问某个页面(使用页面 ID)?

例如:

if ( current_user_has_access(page_ID) ) { DO SOMETHING } else { DON'T }

谢谢。

最佳答案

我不确定这是否有帮助,但这是我的看法。我首先检查所有用户的事件成员(member)资格,然后检查内容 $rules 以查看受限计划是否是用户成员(member)资格计划的一部分 (in_array)

function can_user_access_content($user_id,$post_id){
    //check if there's a force public on this content    
    if(get_post_meta($post_id,'_wc_memberships_force_public',true)=='yes') return true;
    $args = array( 'status' => array( 'active' ));
    $plans = wc_memberships_get_user_memberships( $user_id, $args );
    $user_plans = array();
    foreach($plans as $plan){
        array_push($user_plans,$plan->plan_id);
    }
    $rules = wc_memberships()->get_rules_instance()->get_post_content_restriction_rules( $post_id );

    foreach($rules as $rule){
        if(in_array($rule->get_membership_plan_id(), $user_plans)){
            return true;
        }
    }       
    return false;
}

用法如下:

if(can_user_access_content(get_current_user_id(),$post->ID)){
    //do whatever here
}

关于php - WooCommerce 成员(member)资格 : Conditional to check a page access,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37714426/

相关文章:

php - WooCommerce 结帐时的安全性

wordpress - WooCommerce:更改按钮类而不覆盖模板文件

php - 页脚上的 Nofollow 链接

php - 更改 wordpress the_content() 帖子的字体颜色

php - Kohana 3.2自定义错误处理

javascript - Chrome 扩展 - 使用托管的 PHP 脚本处理输出

html - 将 CSS 框移动到图像的下方

ajax - 使用ajax动态更新woocommerce购物车图标

php - 使用 PHP 通过 IMAP 连接到 Gmail - SSL 上下文失败

php - 在不读取文件的情况下检查文件是否存在于不同的域中