How To Create a New Role in WordPress?

WordPress integrates six default roles: Administrator, Editor, Contributor, Author, subscriber and sixth multi-role: Super Admin which have all privileges under WordPress blog.

In this tutorial we will see how to create a new role for your website subscribers and how to display the content according to the new role.

Why would you need to create a new role in WordPress blog?

We could give you a lot of reason to create a new role for subscribers. However in this tutorial we will give you one good example on how to keep the default subscriber role but also create a premium subscription to monetize your blog. So if you have a blog where you want to give access to certain articles or special offers only to people to have paid monthly subscription for example, they can have this custom-built new role in WordPress blog.

To create this new role (Premium Subscription) in WordPress you will need to add the following code in function.php file of your WordPress theme:

// Add a role
add_role('premium_user', 'Premium Subscription', array(
'read' => true, // true : Allow reading of article page
'edit_posts' => false, // false : Forbidden to add articles or pages
'delete_posts' => false, // false : Forbidden to remove articles or pages
));

Add_role : premium_user identifier will be our new role and Premium Subscription is the name of the role that I attribute to my future subscribers who want this subscription.

Note: When you choosing name and identifier for your new role in WordPress you must not have accents or special characters because you may have an error message which can only be solved from your database, so pay attention which name you typing in. Here are explanations for above commands we added inside function.php file:

  • read: true – we authorize the readin
  • edit_post: false – we will not allow to user with this new role a possibility to add an article
  • delete_post: false – we will not allow to user with this new role a possibility to delete an article

You can see WordPress codex of all other permissions which you can assign. There is few dozens of them, and they are also nicely presented inside the table so you can easily see what they mean.

Now once we have created the new role, we will need to define what content will be displayed for users with this role. In order to achieve that we will need to create new a page template that we will call for example ProUsers.php. Now inside that empty file add following code:

<?php
/**
* Template Name: Premium
*
* Page for users with premium membership
*/
get_header(); ?>
<div id="container">
<div id="content" role="main">
</div><!-- #content -->
</div><!-- #container -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Now we are going to add function called current_user with the identifier of the new role premium_user and the administrator administrator. This function will allow only members with “Premium Subscription” and WordPress blog administrator to access the content of this page.

<?php global $current_user;
if ($current_user->roles[0] == 'premium_user' || $current_user->roles[0] == 'administrator' ) {
echo 'You have Premium Subscription';
// The contents posted for premium members
} else {
echo 'You dont have Premium Subscription';
}?>

To delete a role in WordPress simply add the following line in your function.php file of your theme indicating its identifier:

remove_role( 'author' );