Build a membership site that rewards members for signing up earlier rather than later. This can be done by preventing new members from seeing published content that was published before a members join date.

Hide Previous Content From New Members | Paid Memberships Pro

About the Code Recipe

In some cases, you may want to prevent new users from seeing older content on your membership site. This is especially true if your content is very serial in nature or if you want to generate a more exclusive feel for your membership site. This code will create a content filter for all pages and posts to remove access to posts that were published before a member’s join date. Only the posts or pages which require membership will be hidden.

Note: Pages and posts that require a membership will still be hidden from non-members regardless of the publish date.

The Code Recipe

/*
This code will create a content filter for all pages and posts to remove access to posts that were published before a member's join date. Only posts or pages which require membership will be hidden. Note that pages and posts that require membership will still be hidden from non-members regardless of the publish date.
The params passed are:
$hasaccess - (bool) what PMPro thinks about whether the user has access
$thepost - (post object) the post being checked, usually the current post
$theuser - (user object) the user being checked, usually the current user
$post_membership_levels - (array of levels) the levels this post requires (if any)
*/
add_filter("pmpro_has_membership_access_filter", "hide_old_posts_from_members", 10, 4);
function hide_old_posts_from_members($hasaccess, $thepost, $theuser, $post_membership_levels)
{
global $wpdb;
//if PMPro says false already, return false
if(!$hasaccess)
return false;
//if the post doesn't require membership, allow access
if(!$post_membership_levels)
return true;
//okay, this post requires membership. start by getting the user's startdate
$startdate = pmpro_getMemberStartdate($theuser->ID);
//no startdate? return false
if(empty($startdate))
return false;
//if the startdate is before the post date, return true
if($startdate < strtotime($thepost->post_date))
return true;
else
{
//in this case we want to also tweak the message shown
add_filter("pmpro_non_member_text_filter", "swap_old_posts_member_text");
return false;
}
}
function swap_old_posts_member_text($s)
{
$s = "This content was published before your membership started.";
return $s;
}

Adding the recipe to your website

Copy and paste this code recipe into a helper PMPro Customizations plugin.

Free Course: Membership Site Development—The Basics

Develop a deeper understanding of membership site development in this beginner-level course. Learn how to make your site work better, save yourself time and money, and improve your site's performance.

Featured Image for Membership Site Development Course: The Basics


Was this article helpful?
YesNo