2

I have a Wordpress Memberships website that is built on WooCommerce with WooCommerce Memberships plugin to restrict certain pages to members only.

Some of those pages are "drip-fed"... ie. Access to those pages opens 3 days after purchase, etc. I have set this up in WooMemberships.

I am trying to simply do a PHP conditional check to see if the current user has access to a certain page.

I have found this code piece in the docs: wc_memberships_is_post_content_restricted()

However, I have been unable to make it work.

Is there a code snippet which will basically do a PHP IF statement on whether the current user has access to a certain page (using page ID)?

eg:

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

Thanks.

mujuonly
  • 11,370
  • 5
  • 45
  • 75
Scott
  • 57
  • 1
  • 10

3 Answers3

4

I'm not sure if this helps but here is my take on it. I first go through all of the users active membership and then I check the content $rules to see if the restricted plans are a part of the users membership plans (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;
}

Usage would be something like:

if(can_user_access_content(get_current_user_id(),$post->ID)){
    //do whatever here
}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • I added in if the content is resticted at all: `function can_user_access_content($user_id,$post_id){ //Check if this is restricted at all if(wc_memberships_is_post_content_restricted($post_id) === false ) return true; }` – vimes1984 Aug 30 '18 at 08:57
4

I'm dealing with the same issue at StoryMoment.com (we produce audio + eBook story series for kids).

This is how I've dealt with it. I use the following in a page template to show or hide page elements based on access. The wc_memberships_view_delayed_post_content would change based on the type of content.

You can see the other options in the file:

class-wc-memberships-capabilities.php

<?php 

$has_access = current_user_can( 'wc_memberships_view_delayed_post_content', $post->ID );

if ($has_access) {

//do something

} else {

//do something else

}

?>
Evan Brammer
  • 61
  • 1
  • 6
  • 1
    Agreed. This is super simple and works perfectly. Just wondered how / if it's possible to add the Memberships built in "restricted content" message to the `else` section? – Trevor May 24 '17 at 00:51
1

You will have to Replace (in the conditions):

  1. $page_id by your page ID number (for example: is_page(42))
  2. $membership_plan by the slug of the plan ('plan_slug') or related post ID.

The conditions:

  • wc_memberships_is_post_content_restricted($page_id) => true if $page_id is retracted.
  • is_page($page_id) => true if is actual $page_id.
  • wc_memberships_is_user_active_member( $membership_plan ) => true actual user is an active member for this $membership_plan plan. In that case the access to the page is granted by the suscription plan of the user.

You can remove some of the conditions, if not needed, and fine tune for your needs.

if( wc_memberships_is_post_content_restricted() && is_page($page_id) && wc_memberships_is_user_active_member( $membership_plan ) ) {

    // do something

} else {

    // don't

}

--- Update ---

The only function related to restriction and (or) time access are:

1) wc_memberships_restrict( $content, $membership_plans, $delay, $exclude_trial )
just like shortcode [wcm_restrict] (so not useful)…

2) wc_memberships_get_user_access_time( $user_id, $target, $action, $gmt ): Parameters

$user_id  // for a logged 'user ID'
$target   : array('post' => id, 'product' => id) // content_type and content_id
$action   : 'view' or 'purchase' // Type of access (products only)<br>
$gmt =>   : true  or  false // (selection of the time zone)
// Returns user access start timestamp (in site timezone) for content or a product

Reference: WooCommerce Memberships Function Reference

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • That is amazing thank you. I've tried it out, and it is confirming that the user is a member, etc - however it is saying they have access to pages that they should not have access to until X days have passed... Is there a way to check the member has access to a certain page, based on their sign up date (I have set in Woo Memberships that pages should only display after X days)?? For context - I'm creating a menu that only displays the pages they currently have access to. – Scott Jun 09 '16 at 01:22