Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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

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.

The post How PHP helps in changing User Roles and Capabilities in WordPress? appeared first on .



This post first appeared on 5 Best Image Optimize Plugin For Your WordPress Website, please read the originial post: here

Share the post

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

×

Subscribe to 5 Best Image Optimize Plugin For Your Wordpress Website

Get updates delivered right to your inbox!

Thank you for your subscription

×