Justin Tadlock

How I run a membership site

I get several emails weekly about how I handle memberships on Theme Hybrid. After a few years of these emails, I figured it was about time to share how I do things. You’ll probably be surprised how simple it is.

In this tutorial, I will be covering how I handle membership after the initial registration and payment. The current plugin I use for the payment system is only held together by the numerous code fixes I’ve had to put together and is not something I’d recommend to others.

However, I’ve used Gravity Forms, plus its User Registration and PayPal add-ons, successfully on numerous other sites to handle registration and payment. I highly recommend using this plugin if starting a paid membership site.

Required plugins

Any good membership site (free or paid) needs a way for the admins to handle permissions. This is why I originally created the Members plugin. Members is basically just a UI layered over what’s already available in WordPress. It gives you access to WordPress’ built-in roles and capabilities, creating a powerful and robust permissions system, especially if you plan to have any custom permissions.

Members also has some other features baked in, but I don’t really use them. I use the main role editor for my site because it’s all I need for handling content.

You don’t need any other plugins unless you need a way to handle payments. In that case, go with Gravity Forms as I mentioned earlier.

The rest of this tutorial will assume that you’re using the Members plugin.

Creating a new capability

On Theme Hybrid, you must be a member of the club to view certain parts of the site, such as plugin and theme documentation. WordPress doesn’t have a good, built-in way to hide these parts of the site by default. So, I created a new capability (i.e., permission) called view_club_content. Basically, this capability decides whether a user can view content that’s restricted to club members.

The first step is creating this capability and adding it to the Administrator role because I, the administrator, should be able to view club content. I do this by the following steps.

  • Go to the “Users > Roles” screen in the admin.
  • Click on the “Administrator” role.
  • Scroll down to the “Custom Capabilities” section and click “Add New.”
  • Type view_club_content in the input box.
  • Click the “Update Role” button.

That’s all it takes to create and add a new capability to the role. Now, any site administrators will have access to club content.

The following screenshot shows how this is done.

Screenshot of adding a new cap in the Members plugin

Creating a club member role

All club members are given the “Club Member” role. I separate paying members from non-paying members by giving them different roles (actually, I have several roles for other purposes not being covered in this tutorial). Since WordPress doesn’t have a Club Member role by default, I need to create one.

The following steps describes how to create the custom role.

  • Go to the “Users > Add New Role” screen in the admin.
  • Type club_member in the “Role Name” input box.
  • Type “Club Member” in the “Role Label” input box.
  • Select the view_club_content capability from the capabilities list.
  • Click the “Add Role” button.

The following screenshot shows how a new role is added.

Add New Role screen of the Members plugin

That’s how easy it is to create a new role. It’s also all the functionality I really need from the Members plugin.

Hiding club member content

The Members plugin has a component called “Content Permissions” that you may enable to hide content on a post-by-post basis. While that’s a cool feature, it doesn’t work well for those times when you want to hide groups of content, such as all tutorials. For that, I use a little custom code.

I’m going to keep this pretty simple. The actual code for my site is a bit more complex but not by much.

What I do is create a custom template in my theme called club-content-no.php. This template is shown on any page that the current user can’t view. The template code is similar to the following.

<?php get_header(); // Loads the header.php template. ?>

    <div id="content">

        <div class="hfeed">

            <h1 class="entry-title">Club Member Content</h1>

            <div class="alert">
                <?php if ( !is_user_logged_in() ) { ?>
                    <p>This page can only be viewed by exclusive members of the Theme Hybrid club.  If you're already a member, please take a moment to log into the site.  If not, please consider joining our awesome community of people.  Just head on over the the <a href="/club">club page</a> and sign up!</p>
                <?php } else { ?>
                    <p>This page can only be viewed by exclusive members of the Theme Hybrid club.  Please consider joining our awesome community of people.  Just head on over the the <a href="/club">club page</a> and upgrade your membership!</p>
                <?php } ?>
            </div>

        </div><!-- .hfeed -->

    </div><!-- #content -->

<?php get_footer(); // Loads the footer.php template. ?>

Now that I have a template, I need a way to show that template to users who don’t have permission to view certain pages. WordPress has a filter hook called template_include that works perfectly in this situation. This hook is executed before deciding and loading the template to use on the current page.

Let’s assume that we don’t want users to see tutorials and theme documentation pages (two custom post types I use). The following code will handle this.

add_filter( 'template_include', 'th3_club_member_template', 99 );

function th3_club_member_template( $template ) {

    if ( is_singular( array( 'tutorial', 'doc_theme' ) ) && !current_user_can( 'view_club_content' ) )
        $template = locate_template( array( 'club-content-no.php' ) );

    return $template;
}

That’s a total of 6 lines of code for the real functionality behind my membership system. This code does a few things:

  • It filters with a priority of 99 to make sure it’s executed after any other filters.
  • The code checks if the user is currently viewing a single tutorial or doc_theme post.
  • It checks if the current user has permission to view_club_content (the custom capability I used).
  • It loads the club-content-no.php template if the user doesn’t have permission to view the page.

It’s easy to run a membership site

Traditionally, in the WordPress community, membership systems have been overpriced, are overly complex, and often work outside of the WordPress roles/caps system. This is another major reason I created the Members plugin. You can download and use it for free.

There’s no magic here. It’s not even something that’s extremely complex for the average site owner. WordPress already has the tools built in. The Members plugin just gives you access to those tools.

I realize that my example is simple. There are much more complex membership sites out there. However, this tutorial should give you a solid base for working within the system and allow you to build whatever type of membership site you need.