Sometimes you may want to limit access to older content on your membership site. This approach works especially well for serial content or communities that reward early members.

This code recipe helps you create a more exclusive experience by restricting access to member-only posts and pages published before a member’s join date. As a result, new members only see content released after they sign up.

Featured image for code recipe on hiding previous content from new members

Understanding the pmpro_has_membership_access_filter hook

Paid Memberships Pro normally allows members to view any post or page assigned to their membership level. You can change that behavior by using the pmpro_has_membership_access_filter hook.

When PMPro checks access, it runs this hook and passes in key details. These include the current access status, the post being viewed, the user requesting access, and the membership levels allowed for that content.

Your custom code can then return true to allow access or false to block it.

About the Code Recipe

This code recipe uses the pmpro_has_membership_access_filter hook to override the default access rules for protected posts and pages.

First, the function confirms that the user has the required membership level. It then retrieves the member’s start date using pmpro_getMemberStartdate() and compares it to the post’s publish date.

If the post was published after the member joined, the code allows access. If the post was published earlier, the code blocks access instead. In that case, the recipe also hooks the my_pmpro_swap_old_posts_member_text() function into pmpro_no_access_message_body to display a custom message explaining why access is restricted.

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

Screenshot showing a Paid Memberships Pro post restricted because it was published before the member’s join date.

The Code Recipe

Adding the Recipe to Your Website

You can add this recipe to your site by creating a custom plugin or using the Code Snippets plugin available for free in the WordPress repository. Read this companion article for step-by-step directions on either method.

Customizing the Code Recipe

To customize the message that’s displayed to members who have been denied access to old posts, edit the content of $s on line 48.

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