If you enjoyed this article, get email updates (it's free).
Join over 77,235 Subscribers Today.
Who doesn’t want to earn money from blogging? I mean, sometimes you have shortages, sometimes comments let you down, payment is not THAT good. But you know, it’s still better than real work, right? (Troll’s comments in 3..2..1..)
So instead of spending a few bucks in order to get a premium membership system, why not create your own? And you can also learn a little bit about WordPress. Big deal, isn’t it? And you will be amazed at how easy is to build such great feature.
This is our first post from 3 and we’ll be talking about user roles, register, show / hide content based on user’s role.
So, Let’s Rock!
Our goal here is to get a plugin that allows us to publish premium content (with teasers for non-premium users) and normal content so we will get traffic anyway.
First of all we need to know who is premium member and who isn’t. WordPress doesn’t have a “premium subscriber” role, but is pretty easy to create it.
If you’re not familiarized with basic plugin structure and planning, you should check out this basic tutorial that I’ve done a couple of months ago.
As you can see we have our basic plugin declaration and a simple snippet that create our brand new user role. To make sure this code is working just activate it (duh!) and go to Wp-admin -> users -> Add New and you should see this in your role select:
Did you notice that we’ve set our premium subscriber with ‘read’ and ‘read_premium’ capabilities? Yeah, we will use this read_premium capability to know if user can / cannot see some content. That’s good, but since we’ve just created this capability (right now!) any other user role (including admin) funnily won’t be able to get access to this premium content.
Now we will give them capability to do so:
This is really simple also, you just need be aware that once you give some capability for users (like this ‘read_premium’) they will have it, unless you remove. So if by mistake you add this capability to subscriber just removing the code won’t remove user capability. But this code will:
Ok, let me tell you a secret now, what we’ve done is actually really bad code. WordPress will run it every time it loads, but this snippet just have to run once (once created role and capability they remain intact until you delete them). So after debugging, let’s wrap all this code inside a register_activation_hook:
Ok, you have your role, but how to create users and add them to this role? Well, we will be talking about payment process later but you can prepare yourself to promote them to premium. Let’s create a function that promote our users (when you have payment, you run it):
But before promoting your users you have to create them, right? So we will prepare our system for doing that outside WordPress default register page, all you have to do is to call this function from anywhere (like from sidebar register, as we will see above):
Let’s boost your conversion rate by creating a simple login / register sidebar form. This time we will need to make some changes in your theme, hope it’s ok with you.
We will need to make some changes in sidebar. Change it as you want, but basic code will be something like this:
So by the end of this point you will have:
Ok, now we need to put functionality on it. We need to add this to our header.php (before DOCTYPE):
Then you will be able to register, login, and logout :)
Now it’s what you were waiting for: how to show and hide stuff based on user role.
We will need a little setup at this point, go to wp-admin -> settings -> reading -> For each article in a feed, show = Summary. Do that so your feed will show only excerpts.
Then at this point we have 2 options, the lazy and the right way. The lazy one is just to add this code to your plugin:
Since this is the lazy option, as always in our lives, it will give more work :D Yeah you will need more setup and trust a lot in your theme developer to do that. But we have a better option, dear fried.
You can go trough your theme and search for crucial files like single.php, category.php, search.php, and why not home.php. Then when you think users can’t see premium content just put this:
This gives you more control, since you may want to give some free content categories ( just add them in your if).
As you can see, this is just our start point, dear Padawan. We will dive a little bit more on that later, but hope you’ve had enough fun for now :)
Join over 77,235 Subscribers Today.
I'm a web designer and entrepreneur from Itajubá (MG), Brasil. I love writing about obscure topics and doing some cool stuff. And also I do some FREE stuff, check it out: http://www.roch.com.br/