In this tutorial, we're going to take a look at how you can leverage several WordPress functions to build a surprisingly robust and extendable membership / gated content / paywall feature for your blog.
We'll be using core WordPress and no other plugins for this example, but you'll quickly see how simple and extendable this solution can become.
Let's say we run a popular WordPress website, and want to offer both public and members only content.
This is a simple, yet powerful snippet that makes use of several awesome WordPress functions.
The conditions that we're checking are:
These conditions are further broken down to:
If the conditions aren't met, the user is sent to their respective page, and cannot access the content.
The hook used is template_redirect which allows us to make this check well before any content is loaded.
I'd also tack on a members only parameter to that redirect,
wp_redirect( '/wp-login.php/?members-only-msg&postid=' . get_the_id(), 301 ), which we can then use to display a message on the login page, giving redirected users context as to why they were sent to this page.
This is a basic example, but we can see that we're using $_GET['members-only-msg'] to check and see if the parameter is set, and if it is, echoing out a message.
We're also customizing it further by passing the ID of the post that the non authenticated user is trying to access, and use that to display the post title that they came from:
And just like that, it's pretty easy to build out a robust, yet simple "members only" system.
All of the code here is unedited CodeWP generations. With the right prompts, we can create some pretty complex paywalls and redirection rules, as shown below:
Prompt: Check if user has bought the a WooCommerce product at least 5 times, has an active subscription, and has had an account for over 3 years. If they meet these conditions, allow them to access WordPress posts with the "members-only" tag. If not, redirect them to the /gold-club/ page
What if you don't want to completely restrict somebody from accessing the post... not members only, but a paywall?
That's also pretty easy to do.
This snippet checks if the content is:
And if the user isn't a member, and the post is categorized for members, they'll get an excerpt of 100 words, and a link to the upgrade page that says "upgrade to read the rest of this post".
You're probably thinking, "this is great, but how can I get the user to get the role of member?".
First, we'll register that role as it's not standard for WordPress:
Now, in the back end, you can add new users as members, or change existing user roles to this.
How about selling membership? Don't you need a plugin for that?
Not really. Instead, let's use WooCommerce and a $5 product called "Support Blog, Become a Member".
This code checks to see if they buy that product (assuming the ID is 5). If they do, the user's role is updated to 'member', and they have access to all of the gated content. Keep in mind, this is a bit simple for paid membership system, but it gets the point across!
Membership and paywall plugins for WordPress are great, but sometimes you just don't need them. This article shows a couple of concepts you can use to implement a simple membership system, using CodeWP generated code.