Best Way to Restrict WordPress Content

To restrict content in WordPress first you need to understand user roles. By default WordPress has five user role. Only administrator has the privilege to access all the administration features. To know more about user roles please check in WordPress codex In this tutorial we will show you how you can restrict WordPress content to specific users by using one of most popular plugin WPFront User Role Editor .At first download the plugin and install it to your WordPress site.

So lets try to understand full process. For example you have some premium content and you want to share with different user group. So we are going to create a custom post type “Premium Posts” with custom taxonomy “User Roles”. Here is the plugin:


 * Plugin Name: MTG Premium Content
 * Plugin URI:
 * Description: A simple plugin that adds premium contents.
 * Version: 0.1
 * Author: My Tutorial Guru
 * Author URI:
 * License: GPL2

/*  Copyright 2014  My Tutorial Guru  (email :

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 2, as
    published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

function my_custom_posttypes() {
    $labels = array(
        ‘name’               => ‘Premium Posts’,
        ‘singular_name’      => ‘Premium Post’,
        ‘menu_name’          => ‘Premium Posts’,
        ‘name_admin_bar’     => ‘Premium Post’,
        ‘add_new’            => ‘Add New’,
        ‘add_new_item’       => ‘Add New Premium Post’,
        ‘new_item’           => ‘New Premium Post’,
        ‘edit_item’          => ‘Edit Premium Post’,
        ‘view_item’          => ‘View Premium Post’,
        ‘all_items’          => ‘All Premium Posts’,
        ‘search_items’       => ‘Search Premium Posts’,
        ‘parent_item_colon’  => ‘Parent Premium Posts:’,
        ‘not_found’          => ‘No Premium Posts found.’,
        ‘not_found_in_trash’ => ‘No Premium Posts found in Trash.’,
    $args = array(
        ‘labels’             => $labels,
        ‘public’             => true,
        ‘publicly_queryable’ => true,
        ‘show_ui’            => true,
        ‘show_in_menu’       => true,
        ‘query_var’          => true,
        ‘rewrite’            => array( ‘slug’ => ‘premium-posts’ ),
        ‘capability_type’    => ‘post’,
        ‘has_archive’        => true,
        ‘hierarchical’       => false,
        ‘menu_position’      => 5,
        ‘supports’           => array( ‘title’, ‘editor’, ‘thumbnail’ )
    register_post_type( ‘premium-post’, $args );
add_action( ‘init’, ‘my_custom_posttypes’ );

// Flush rewrite rules to add "review" as a permalink slug
function my_rewrite_flush() {
register_activation_hook( __FILE__, ‘my_rewrite_flush’ );

// Custom Taxonomies
function my_custom_taxonomies() {
    $labels = array(
        ‘name’              => ‘User Roles’,
        ‘singular_name’     => ‘User Role’,
        ‘search_items’      => ‘Search User Roles’,
        ‘all_items’         => ‘All User Roles’,
        ‘parent_item’       => ‘Parent User Role’,
        ‘parent_item_colon’ => ‘Parent User Role:’,
        ‘edit_item’         => ‘Edit User Role’,
        ‘update_item’       => ‘Update User Role’,
        ‘add_new_item’      => ‘Add New User Role’,
        ‘new_item_name’     => ‘New User Role Name’,
        ‘menu_name’         => ‘User Role’,

    $args = array(
        ‘hierarchical’      => true,
        ‘labels’            => $labels,
        ‘show_ui’           => true,
        ‘show_admin_column’ => true,
        ‘query_var’         => true,
        ‘rewrite’           => array( ‘slug’ => ‘user-roles’ ),

    register_taxonomy( ‘user-roles’, array( ‘premium-post’ ), $args );
add_action( ‘init’, ‘my_custom_taxonomies’ );


Save this code in a file name mtgpremiumcontent.php and upload to your plugin folder and you will see the plugin in list.

premium post wordpress plugin


Active the plugin and you will see a tab Premium Posts just under Post tab.

premium post wordpress plugin

Now go back to WPFront user role editor plugin. After installing this plugin you will find a menu name Roles:

wordpress user role editors

wordpress user role editors

Here we will add two new roles. One is “Group 1” and another one is “Group 2”.  And we will assign capabilities only Dashboard to these user roles. So now go to Add New Role and create both the roles:

Restrict WordPress Content

After creating both the group you will see the role in All roles list as follows:

wordpress user role editorNow add 2 new user to WordPress and while selecting role please “Group1” for one user and “Group2” for another user.

wordpress user role editorNow let’s go back to Premium post plugin menu. Go to Premium Posts-> User Role menu:

premium post wordpress plugin


And add two new group “Group1” and “Group2” (same group we have created earlier in user role);

premium content wordpres plugin


Now go to Premium Posts-> Add New menu and try to add a new post. In here in right column you can see there is a User Roles tab and all the roles we have added earlier are listed:

user role editor wordpress plugin

Just select a single group or multiple group while your posting a new content and the content will be only visible to that particular user group:

restricted content wordpress plugin


So back-end work is done, now the question is how to show all this premium content in front end? Here is the solution:

<?php if ( is_user_logged_in() ) { ?>
<a href="<?php echo wp_logout_url( get_permalink() ); ?>" title="Logout"><h2>Logout</h2></a>
$current_user = get_current_user_id();
$user_info = get_userdata($current_user);
$roles= implode(‘, ‘, $user_info->roles);// get the role of current user
global $paged;
$paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
$curpage = $paged ? $paged : 1;
$args = array(
‘post_type’ => ‘premium-post’,
‘tax_query’ => array(
‘taxonomy’ => ‘user-roles’,
‘terms’ => $roles, // checked whether current post has been assigned to current user role or not
‘field’ => ‘slug’,
‘operator’ => ‘IN’
‘order’ => ‘DESC’,
‘posts_per_page’ => 5,
‘paged’ => $paged

$query = new WP_Query($args);
if($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
//retrive yor content
<?php endwhile; endif; ?>

//send him to login form

Here is the login form:


<form action="<?php echo get_option(‘home’); ?>/wp-login.php" method="post">
<table width="100%" class="table_box_con">
<td>User Name:</td>
<td><input type="text" name="log" id="log" value="<?php echo wp_specialchars(stripslashes($user_login), 1) ?>" size="35" /></td>
<td><input type="password" name="pwd" id="pwd" size="35" /></td>
<td><input name="submit" type="submit" value="Submit"></td>
<label for="rememberme"><input name="rememberme" id="rememberme" type="checkbox" checked="checked" value="forever" /> Remember me</label>
<input type="hidden" name="redirect_to" value="<?php echo $_SERVER[‘REQUEST_URI’]; ?>" />
<a href="<?php echo get_option(‘home’); ?>/wp-login.php?action=lostpassword"> <p>Recover password</p></a>

If you think this post is useful please don’t forget to comment.

Author: mytutorialguru

Share This Post On

Submit a Comment

Your email address will not be published. Required fields are marked *