QNimate

  • CoursesVideos
  • WP PremiumPlugins
  • DemosLab
  • Bitcoin BankingBuy, Sell, Store and Earn Interest
  • Home
  • QIdea
  • QTrack
Home Carbon Ads Understanding WordPress User Roles

Understanding WordPress User Roles

wordpress-user-roles

This post is a part 11 of Creating a WordPress Admin Theme post series.

A WordPress site can have multiple registered users. All these users don’t have the same authority for site customisation, posting, editing etc. They all have different set of permissions to deal with the website. In this article I will explain what are user roles and capabilities And how to create customized roles.


Roles and Capabilities

A capability in WordPress is a permission given to a registered user. There many different kinds of capabilities defined in WordPress. We cannot create our own capabilities we can only assign defined capabilities to registered users. Example of some capabilities are: activate_plugins, delete_pages, manage_options, read etc.

A role is a set of capabilities. We cannot directly assign capabilities to a registered user, we always assign role to a registered user. A registered user must have a role. Example of some roles are administrator, editor, author, contributor and subscriber.

We can programatically add/remove capabilities from roles. We can also create our own custom roles.

User responsible for creating the website is by default assigned to administrator role. A administrator has all capabilities. A site can have multiple administrators. Administrators can alter other administrators accounts also.

While registering new users if you don’t provide a role then by default assigned to subscriber role. We can change this default behavior by navigating to Settings → General in admin menu. There you will find a option to change it.
Capture

Here you will find list of predefined user roles and their respective capabilities.

Creating new roles

add_role is used to create new roles.

Put this code in your plugin or theme file:

<?php
    //check if role already exists
    if(get_role("ninja") == null)
    {
        //role doesn't exist create it.
        //parameters: role name, display name, an array of capabilities
        $role_created = add_role("ninja", "Ninja", array(
                'read'         => true,  // true allows this capability
                'edit_posts'   => true,
                'delete_posts' => false, // Use false to explicitly deny
            ));

        if($role_created !== null)
        {
            //role created successfully
        }
        else
        {
            //failed to create a new role
        }
    }
    else
    {
        //role exists
    }
?>

Capture

Remove a role

We can remove a predefined role and also a custom created role using remove_role.

Put this code in plugin or theme file to delete subscriber role

<?php
    //check if role exists
    if(get_role("subscriber") !== null)
    {
        //role exists.
        //parameters: role name
        $role_deleted = remove_role("subscriber");

        if($role_deleted !== null)
        {
            //role deleted successfully
        }
        else
        {
            //failed to delete role
        }
    }
    else
    {
        //role doesn't exist
    }
?>

Adding and Removing Capabilities

We can add capabilities to predefined roles or custom roles using add_cap. Similarly we can remove capabilities from predefined roles and custom roles using remove_cap.

<?php
    //check if role exists
    $role = get_role("author");

    if($role !== null)
    {
        //role exists.
        $role->remove_cap( 'read' );
        $role->remove_cap( 'delete_posts' );

        $role->add_cap( 'edit_pages' );
        $role->add_cap( 'manage_links' );
    }
    else
    {
        //role doesn't exist
    }
?>

Sep 23, 2014Narayan Prusty
Hittail AlternativeHTML Character Codes Table
Comments: 1
  1. julie ranawat
    4 years ago

    Great Work..it’s really useful us..

    ReplyCancel

Leave a Reply Cancel reply

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

Narayan Prusty

I am a full-stack web developer. I specialize in Blockchain and JavaScript. This is my personal blog where I write about programming and technologies that I learn and feel interesting to share.

Image6 years ago 1 Comment WordPress
Share this
0
GooglePlus
0
Facebook
0
Twitter
0
Linkedin
  • Roles and Capabilities
  • Creating new roles
  • Remove a role
  • Adding and Removing Capabilities
Related Articles
  • WordPress Custom PHP, JS and CSS Plugin
  • WordPress Set Visual Editor as Default
  • WordPress Set HTML Editor as Default
  • WordPress Coupon Site Plugin
  • WordPress Disable Automatic Update Emails
Our Sponsor
Freelance: I am available
@narayanprusty
Hi, I am a full-stack developer - focusing on JavaScript, WordPress, Blockchain, DevOps, Serverless and Cordova. You can outsource your app development to me.





Will get back to you soon!!!
WordPress
ReactJS
Meteor
Blockchain
Serverless
Kubernetes
DevOps
DB & Storage
Ejabberd
Let's get started
My Books

2014 - 2015 © QNimate
All tutorials MIT license