Restricting Access to WordPress Pages & Creating a Members Area

By Kirsty Burgoine

Looking to publish premium courses or some informational product? You might want to restrict WordPress content just those readers/members who pay for it. And hide content for general visitors. With WordPress, you can easily protect such information using the various content restriction means that WordPress supports right out of the box or with the help of plugins.

We’ll go over these different methods in this article. We’ll discuss:

  • The difference between password protected and private pages (and where the two solutions fall short)
  • A 2-min hack to create a simple Members Area on your WordPress site (without using a plugin)
  • Getting your knowledge base articles to show up in your Members Area (this section is for you IF you use our KnowAll theme or Heroic Knowledge Base solution)
  • Free plugins that will help you control access to your site’s pages, posts, and other content

Let’s begin by understanding the difference between password protected and private content.

Password protected vs. private

The simplest way to restrict access to a specific page (or other post content) is to use the different ‘Visibility‘ settings that WordPress gives by default.

You can find these options on the right-hand panel wherever you add a new page or post:

Under the Visibility settings, there are 3 options for the visibility of a page (or post):

  1. Public – Under this default setting, content is available to all site visitors
  2. Password protected – Here, only the readers with the page’s (or post’s) password can unlock access to the content
  3. Private – This option grants access to specific users based on their role

The second and third visibility options allow you to restrict certain content.

Let’s see how:

1. Password protecting pages/posts

This is a very straightforward option giving you the ability to password protect individual pages and posts simply by assigning a password to that page (or post).

Anyone who knows the password(s) can access the content.

Now, this option works if you have a page or two that you’d like to hide from the general public. However, you can imagine that if you have 100s of articles to protect, you’ll have to maintain and distribute 100s of passwords with all the eligible readers.

Another problem with this method is that if a user finds such content or clicks on a link to such a password protected page, they will see a screen asking for a password, so they’ll know that there’s some protected or exclusive content on your site. So, if you don’t want the people who don’t have access to learn about this content, then this method won’t solve your purpose.

The following screenshot shows an example of a password protected resource:

Furthermore, password protecting a page doesn’t remove that page from the website if you don’t have permission to view it. Instead, the word ‘Protected:‘ is added to the beginning of the title, which does have its uses, but its not ideal if you don’t want people to know there is private content on the website.

protected-articles

 

2) Marking pages/posts private

Private Pages don’t require individual passwords for getting unlocked. Access to such pages is granted after checking a user’s role. Typically, only site admins or editors can view such private pages.

Here’s how a page marked private looks like:

The other big difference between password protected and private pages is that if you don’t have sufficient privileges to view a page, then it won’t display on the website anywhere.

This is especially useful if you don’t want the average website visitor to know there is additional content on the website.

logged-in-and-not
Left: Private articles are completely hidden if you are not logged in or don’t have the correct permissions. Right: The same articles publicly available.

It’s clear:

Of the two options, only the Private visibility setting makes sense if you want to restrict a lot of content on your site.

But as you can tell, you can’t possibly give every user admin or editor level access on your site. Which means you need to introduce another membership level (subscribers). And then set the private pages’ visibility level to that of subscribers. This way, all your subscribers will be able to access all the restricted content.

Here’s how you can implement this simple access system by setting up a Members Area:

Creating a simple Members Area

First, create a set of pages that will only be available to the members. Make all these pages private.

Now because by default, only users with the editor or admin level access can access these pages, we need to make this page visible to the subscriber-level user as well.

To do this, you need to add a new function and action to the functions.php file in your theme.

So, go to the functions.phpfile in your theme and add the following code to it:

 

/**
* Add read_private_posts capability to subscriber
* Note this is saves capability to the database on admin_init, so consider doing this once on theme/plugin activation
*/
add_action ('admin_init','add_sub_caps');

function add_sub_caps() {
	global $wp_roles;
	$role = get_role('subscriber');
	$role->add_cap('read_private_posts');
}

All this does is change the capabilities (permissions) assigned to the Subscriber role to include the ability to read private posts. Note that this is a one time action, so it’s probably better to perform on theme/plugin activation, or comment out the code when done. See the Codex’s guidance notes on add_cap. Also, if you want to remove the capability, you’ll need to change the code to use the remove_cap function.

Setting up login for the Members Area

Now that your Members Area is ready, it’s time to make it easy for the subscribers to sign in. Of course, your users can use the yourwebsite.com/wp-login.php link, but you’ll agree that this isn’t very user-friendly.

To setup user-friendly login boxes, you can use the Login page styler plugin.

Login page styler lets you design login pages for your WordPress site. You can simply add an item called ‘Login’ to your main menu, and link it to the custom login page designed with Login page styler.

There are a few other plugins as well but most of them haven’t been updated for at least a year, so be sure to check they are compatible before using them.

Once you make your private content available to your subscribers via a friendly login screen, you should be done.

In the next section, we’ll see how you can get your Knowledge base articles (powered by Heroic Knowledge Base) to show up in your Members Area.

Getting the Members Area to include articles from Heroic Knowledge Base

If you use our KnowAll theme or Heroic Knowledge Base plugin to power your website’s knowledge base, then you can make your support content accessible to your subscribers right in the Members Area!

Before you read further, go back to my earlier article where I showed how to use our Heroic Knowledge Base plugin with WooCommerce. It will be a good refresher because, in this walkthrough, I’m going to reference some of the code and the custom field group we created in that post.

Begin by installing the Advanced Custom Fields plugin.

Previously, we had created a custom field group called ‘Product Options‘.

For this tutorial, I’m renaming ‘Product Options’ to ‘Knowledge Base Categories‘ The next thing to do is edit this group to include both pages and posts. Right now, the custom field is only set to display if the post type is a product. But we now want to be able to apply it to pages as well.

To do this, go to Custom Fields in the admin, and edit Product Options.

custom-fields

Once set up, create the knowledge base articles that you intend to only make available to paid members. Set all these pages to private so they can’t be accessed directly. Assign them to a category, say ‘Restricted Content Category‘. Later, you can select this category to be displayed on your restricted pages.

page-edit

The final step is to amend the page.php file to display the selected category.

For doing this, we’ll go back to the function we created to populate a third WooCommerce tab with Knowledge Base articles in my earlier post. You can reuse that here. Just echo the function in page.php in the child theme to display the selected category of articles for any given page.

 

Your page.php code should look like:

<?php get_header(); ?>

<div id="primary" class="content-area">
	<main id="main" class="site-main" role="main">
	
	    <?php 
			//loop start
			while ( have_posts() ) : the_post(); 
				//include the page content template. 
				get_template_part( 'template-parts/content', 'page' ); 

				//check and load the woo_knowledge_base_content
				if( function_exists( 'woo_knowledge_base_content' ) ){
					echo woo_knowledge_base_content();
				} else {
					echo 'function woo_knowledge_base_content() missing';
				}
				 
				//load comment template
				 if ( comments_open() || get_comments_number() ) { 
				 	comments_template(); 
				 } //loop end 
			 endwhile; 
		?>
 
	</main><!-- .site-main -->
		<?php get_sidebar( 'content-bottom' ); ?>
 
</div> <!-- .content-area -->
 
<?php get_sidebar(); ?>
<?php get_footer(); ?>

The only line changed is

echo woo_knowledge_base_content();

Your page should now include the selected category of knowledge base articles and would look something like:

page-final

With this, you should have successfully displayed articles from your knowledge base inside your website’s Members Area.

A few free plugins to restrict content on a WordPress site

If you don’t want to code, consider installing one of the following plugins. All of them can help you protect your content from unregistered users.

Paid Memberships Pro

With Paid Memberships Pro, you can ensure that only your members get access to the different components of your website like pages, posts, categories, forums, downloads, support content, and more.

s2Member

s2Member is another lightweight WordPress plugin that allows content restriction. Using s2Member, you can restrict access to your posts, pages, categories, tags, downloadable files, forums, and other files to just your registered users.

Simple Membership

Simple Membership comes with a friendly dashboard that allows you to protect your members-exclusive content like posts, pages, photo galleries, attachments and more from general site visitors.

In addition to these, you can also check out WP-Members and Membership & Content Restriction – Paid Member Subscriptions. Even these plugins help you lock premium content allowing access to just registered users.

Wrapping it up…

Depending on your goals, you might need anything between basic to advanced content restricting capabilities. But now you know how to implement a simple Members Area yourself. You even know the best plugins to choose from.

So try the code or the plugins and share your experiences below.

 

Leave A Comment?