Create Your Own Premium Membership WordPress Blog – Part 1/3

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!

How will be our plugin

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.

Basic functions:

  • Create a new user role – similar to subscriber but able to read premium content
  • Create users and add them to this role – sometimes WordPress default register form isn’t enough
  • Sidebar login / register – cool feature that may boost your conversion
  • Show / hide content based on user role – so premium will see almost everything :)
  • PayPal integration – Part 2
  • Create and control external db (Payment control)- Part 2
  • Manage cron jobs and wp-cron – Part 2
  • Different menus for logged in users – Part 2
  • Wp-Admin theming – Part 3

Create a new user role

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:


Create users and add them to this role

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):

Sidebar login /register

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:

  • If logged in, logout option and admin link if you’re admin
  • If not logged in, login and register form
  • Any error will show also

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 :)

Show / hide content based on user role

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).

So, what do you think?

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 :)

Rochester Oliveira

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/

15 Smart Tools To Help You Build Your Freelance Business

Discover the awesome tools we use in making our clients comfortable and happy in learning new things every day.

Download Now

Comments

  1. says

    Very useful article. Thumbs up man. But I am stuck at payment gateway integration. I need to integrate Authorize.net with it and I can’t find your next parts of this series. Are they covered yet?

  2. Lucas Rolff says

    I was wondering when part 2 will come, to see if I can get it working at that time :D Still sitting with my ugly problem lol

  3. Lucas Rolff says

    As soon as I added the if ! current_user_can read premium, with the add_filter and function in the “plugin” folder.. Then I get a error 500 – if I comment out the “if” my site loads again.. have triple checked the code.

    Can anybody help with this?

    Best regards,

      • Lucas Rolff says

        if(!current_user_can('read_premium'))
        	{
        		add_filter('pre_get_posts', 'remove_premium');
        		function remove_premium($query)
        		{
        			global $post;
        			
        			//so your home, category, tags should show only excerpts, right?
        			if(is_single())
        			{
        				$post->post_content = $post->post_excerpt . "<br />You can't see full text! be premium now!";
        			}
        			
        			return $query;
        		}
        	}
        
        • says

          PHP seems to be right…

          This is inside your functions.php, right?

          Do you have you ‘read_premium’ capability in any role? try logging in with it.

          Error 500, kinda strange..

          []’s

          • Lucas Rolff says

            Hmm, I think I lost the structure of it then :S got lost in the code then, I haven’t added anything to a functions.php file, only that “k-premium.php file I created in the plugin folder, it’s the only file I have in the plugins, and then I edited header, sidebar.

            The reason, why I did the tut, is generally because I’m looking for a membership part of my site, that works well, and I don’t wanted to spend a lot of moneys in it, and then you paste this awesome tut, and I just think like “WoW, Finally a awesome person!”

            Best regards,

          • says

            Your own plugin file should work, also. Don’t know why I thought you were doing via functions :D

            That’s really strange, man. My last guess is any conflict with your theme or another function.. But it would throw a php error, not an 500 (internal server error)

            If you want, send me an email (rochesterj at gmail dot com) with your file and I’ll try it in my own server (where my original code works).

            Anyway we’ll provide a complete plugin file by the end of this series, if you can for it :)

            []’s

          • Lucas Rolff says

            Hello,

            I just think I will wait for the ‘final’ release of the plugin :D

            Anyways, there is a mail to you in your inbox!

            Thank you for the help

        • says

          The reason you get this error is that you cannot call ‘current_user_can’ until later in the WordPress pipeline. Use something like


          add_action( 'init', 'my_initialize' );
          function my_initialize() {
          // put the code in here.
          }

  4. Simon says

    Great thanks, looks like it’s going to be a very useful series, especially the paypal bit :-)

    Just one suggestion: maybe the code you put before the doctype might be better in a function that is called from a hook like init? That way the template file stays nice and clean.

    • says

      Hey Simon,
      This is how I usually do it but you know it’ll be a little harder to explain about hooks inside this one (maybe it deserves one series for itself :D)

      But it’s a good idea, indeed, if you know how to use it (or need some help) I think you should go on it!

      []’s

  5. Aron says

    It’s nice to know that you wrote this tutorial series :)
    I wonder if we can customize the login / register form. For instance: adding more form fields (phone number, email), simple captcha, etc. and capture them to the database as downloadable CSV maybe?
    Really curious of how to do that actually!

    Thank you!

  6. Syed says

    I just started diving into the premium membership sites based on wordpress platform & I am not a fan of third party tools, so I try to avoid as much as I can to reduce dependencies. But of-course adopt one if its the only choice. So I was looking for a custom solution for premium site, but was not satisfied by plugins available in market, and then I found your article its really nice, I liked it and I am hopefully if my client agrees going to follow your approach. Though I might not be able to take advantage of 2 or 3 post but I really liked that you put the table of contents at top, so I can try rest myself.

    Once again, thanks for such a good article, these kind of articles inspire people like me to achieve desired output with control & command.

    Cheers :)

    • says

      Hi Syed,
      Actually I’m not a fan of third party tools too. Many times you need a simple customizations and just can’t when you depend on someone else.

      I hope these functions help you creating your own premium membership blog and if you need any other function just let me know and maybe we add one more part to this series :)

      Thank you!
      []’s

  7. Cat says

    Good stuff! This is going to be EXTREMELY helpful – this is bookmarked now! Can’t wait for the next parts! (=^_^=)

  8. Krrish says

    Useful Tutorial… But Useless for me in Present Stage… You’re really an good programmer.

  9. Andrew Groat says

    Very comprehensive. This is going to be useful for an upcoming job. I was just going to go with aMember but to be honest I think this will work out better :-) (not to mention more lightweight and less costly)

  10. Shaquil Oliver says

    It would be interesting to make money off of blogging. I can help people express themselves in such a way, and to make a profit from it will be a bonus.

    • says

      Hey Shaquil,
      Surely premium membership is a good business model, isn’t it? But the secret is to have a good balance between income sources (ads, affiliate, premium membership..).

      []’s