Introduction

Overview

In this article I will show you how to make content available / unavailable to MemberPress members based on their subscription date.

To get a clearer understanding of this feature, consider the following example:

Let’s say that you have a membership in your MemberPress called Platinum Membership. And let’s say you’re constantly publishing new pages and posts full of juicy content for your Platinum members.

What we want to achieve is to give your Platinum members access to these new pages as you add them—only if they were published after the member signed up.

In other words, if a member signed up on 15th of April 2019, he will not have access to any of the pages that were added and protected under the Platinum Membership before the this date. But if a new page is added to the Platinum Membership (using MemberPress rules) on the 16th of April 2019—one day after this member signed up to Platinum—then he will get instant access to it because it falls after the member’s join date.

Basically this is what’s called a rolling content access window. And it’s perfect if you sell “stacks” of content or plugins (think AppSumo). In fact, one of our customer does exactly that, which is how we came up with this solution and this article. 🙂

Requirements

1 – MemberPress as the membership solution for your WordPress website.

2 – Tag Pages WordPress plugin – this plugin is needed for when we will create the MemberPress rule. It allows us to add tags to pages in the same way that we can add them to posts.

*Note: while this functionality is possible with WordPress posts and custom post types, this tutorial depends on the use of WordPress Pages (because they allow us to easily select a custom template). If you need to implement this differently from how we describe it in this tutorial, get in touch and we’ll be happy to help out.

Set up the membership in MemberPress

1 – Go to MemberPress > Memberships and click on “Add New” top button.

2 – Then on the membership page, add the price of the membership and any of the other settings you need for this product. (The billing type can be “Recurring” or “One-Time”. It does not matter, because the solution I’m presenting here will work with any of them!)

Create the rule in MembePress

I have chosen to create the rule based on the pages tags and that is why I needed the Tag Pages plugin.

1 – First you need to go to Pages > Tags and create the tag which we will need for the rule.

2 – Then go to MemberPress > Rules and click on “Add New”.

3 – On the Rule details page, under Protected Content, select “Pages Tagged” from the drop down and type tag name in “Begin Typing Title”.

4 – Then select “Membership” under Access Conditions and also “Platinum Membership” from the new drop down.

So, when you will add a page under “Platinum Membership”, you will just add the “platinum” tag to it and the rule we have created will be applied.

If you do not want to use it, you can create the rule using “Child Pages of” and add all pages that you want the “Platinum Membership” to protect, as child pages of a single page.

Create page template

At this point we need to create a php template and add it to the theme files.

This template will need to be used on every page protected by the “Platinum Membership” because it has a special php programming added to it which will help us to achieve this functionality.

1 – To do this, you just need to create an empty .php file with any text editor. Open your favorite text editor, save the blank page as a .php file (override the default extension). You can name it whatever you want.

2 – Then through FTP navigate to your WordPress => Themes => Your Theme directory via FTP and call it whatever you want (“platinum.php”, or “PageWithoutSidebar.php”, as in the screenshot below. It doesn’t matter what you call it as long as the file extension is .php) 

3 – Now open the file and add the following code at the top.

php

<?php
/*
Template Name: Platinum
*/
?>

4 – Then, below the code you just added, you will need to add the following php code.

(The following snippet basically forbids access to the page if the user has signed up after the page was added under “Platinum Membership”. I have added comments on most of the code so that things are clear about what it’s doing.)

php

<?php
global $post;

if ( is_user_logged_in() ) {

//get user info
$user = MeprUtils::get_currentuserinfo();

//get rules for the current page
$rules = MeprRule::get_rules($post);

//get memberships which protect current page
$access_list = MeprRule::get_access_list($post);
$memberhips_ids = $access_list[‘membership’];

//get registration date of the user
$registration_date = strtotime($user->user_registered);

//get page creation date – if page date is later then user subscribed date, then user can see the page, if page creation date is sooner than user sign up then user cannot see it
$post_date = strtotime($post->post_date);

//get user’s current memberships
$active_product_subscriptions = $user->active_product_subscriptions();

//check if user has access to the current page
$user_has_access_to_page = array_intersect($active_product_subscriptions,$memberhips_ids);
$user_has_access_to_page_count = count($user_has_access_to_page);

if($user_has_access_to_page_count === 1) {

//replace 2166 in the below code with your membership ID!
if(in_array(“2166”, $user_has_access_to_page) && ($post_date < $registration_date)) {

//replace the below link with the link on your website where you redirect users which are not authorized to view a page
header(“Location: https://yourwebsite.com/unauthorized-access/”);
exit ();

} elseif (in_array(“2166”, $user_has_access_to_page) && ($post_date > $registration_date)) {
get_template_part( ‘page’ );

}
elseif (!in_array(“2166”, $user_has_access_to_page)){
get_template_part( ‘page’ );

};

} elseif (($user_has_access_to_page_count > 1)) {
get_template_part( ‘page’ );

} elseif ( current_user_can( ‘manage_options’ ) ) {

get_template_part( ‘page’ );

}

} else {

get_template_part( ‘page’ );
}

5 – Make sure to replace “2166” in the code above with your membership ID and change the boilerplate link (https://yourwebsite.com/unauthorized-access) to your actual unauthorized access page.

6 – After this .php file is done, you will need to upload it to your theme folder via FTP or SFTP ( yourdomain.com/wp-content/themes/your-theme/ ), if you haven’t already done so. 

(For FTP we use CyberDuck but you can use whatever FTP client you prefer).

How to use this feature

At this point we are almost done!

You only need to do two more things to make this feature work:

1 – Add the tag you created to each new page when you publish it via the “tags” widget in the page editor:

2 – Also be sure to assign each new page to the new template we have just created by using the dropdown in the Page Attributes widget.

I really hope this will be useful to you and please let me know if you have any questions or suggestions in the comments section below!

Share This