QUOTE
 

News & Resources

How PHP helps in changing User Roles and Capabilities in WordPress?

User Roles and Capabilities
Posted by YDS on December 1, 2015 6 comments

WordPress is known to come with a powerful yet simple user management system where users have capabilities which are based on the specific roles they are assigned to. WordPress comes with the following five default user roles and capabilities:

– Administrator
– Editor
– Author
– Contributor
– Subscriber

You can also see full list of user roles on wordpress.org

All these roles come with predefined capabilities but they can be easily changed with the help of coding or plugins such as Members, Capability Manager Enhanced or User Role Editor. These plugins are great to use for wordpress users, who don’t want to get into coding stuff. But if you think you are in a “pro-developer” mode then few lines of PHP will help you out in this.

Change WordPress User Capabilities with PHP

A wordpress role can be modified through an accessory function in PHP which is called get_role. Following code can allow Editor to remove_users:

function wps_editor_can_remove_users() {
$editor = get_role( 'editor' );
$editor->add_cap( 'remove_users' );
}

The fetch of an ‘Editor’ Role takes care of the ‘editor’ parameter on the add_cap method. You can make use of this method in your plugins or themes’s function.php. Whatever changes you do are directly stored to the database.

Create New WordPress User Role with PHP

You can use add_role function in PHP to change the capabilities of existing roles. It includes three parameters:

– Role slug-like name – ‘comment_moderator’
– Role display name – ‘Comment Moderator’
– Array of Capabilities for User

Here is example for the code:

register_activation_hook( __FILE__, 'wps_add_roles_on_activation' );
function wps_add_roles_on_activation() {
add_role(
'comment_moderator',
__('Commment Moderator', 'plugin-slug'),
array(
'read' => true,
'edit_posts' => true,
'edit_other_posts' => true,
'edit_published_posts' => true,
'moderate_comments' => true
)
);
}

Remember that roles must only be created when your plugin is activated because these new roles are stored in the database and are anytime reloaded by WordPress. Don’t worry, as nothing will be broken if you have them hooked in the init action.

Change Specific User Capabilities

Sometimes, there is a need to give few or more powers to a specific user. Methods of add_cap and remove_cap can be used to change a specific user’s capabilities in WordPress. Here is an example for this:

add_action( 'init', 'wps_david_cant_edit' );
function wps_david_cant_edit() {
$user_id = 7; // The ID of the user
$user = new WP_User( $user_id );
$user->remove_cap( 'edit_posts' );
}

Once the role’s general capabilities have been loaded, this is a specific override on a specific user.

WordPress comes with pre defined set of user roles and capabilities that determines what a user can do on the website. Playing with the User roles and capabilities is just another area that one can explore.

Share on Facebook0Share on Google+6Tweet about this on Twitter0Share on LinkedIn2Digg thisShare on StumbleUpon3
 

Comment

  1. Sam says:

    Simply wish to say your article is as amazing. The clarity to your post is simply nice and i can think you’re knowledgeable in this subject.

    Fine together with your permission allow me to clutch your RSS feed to keep up to date with forthcoming post. Thanks 1,000,000 and please keep up the enjoyable work.

  2. Thanks for any other informative site. The place else may I get that kind of information written in such an ideal manner? I have a challenge that I’m simply now working on, and I’ve been on the glance out for such info.

  3. It’s difficult to find experienced people in this particular subject, but you sound like you know what you’re talking about! Thanks

  4. foolish says:

    Wonderful web site. Plenty of useful info here. I’m sending it to some friends ans also sharing in delicious. And certainly, thanks for your effort!

  5. Great information. Lucky me I found your site by accident (stumbleupon). I’ve book-marked it for later!

  6. Peculiar article, just wwhat I wanted to find.

Leave a Reply

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