Friday, 10 April 2015

How to Add/Remove User Roles in Wordpress

Today I am Going to Introduce all of you with a very interesting feature of WordPress and that's User Roles. If you're running a membership website on WordPress platform, this might be super helpful for you.
Add User Roles in Wordpress



There is a huge list of capabilities that a user role is entitled to perform. For example, Edit post, Delete posts, edit pages, delete pages, change theme, upload plugins, remove plugins, edit plugins etc.  Now in your membership website which is developed on WordPress asking your to define a user role who is empowered to make some changes and can read only a few things in his WP-admin there. Now all you need is adding a new user role. For detailed study on capabilities and roles Goto wordpress.org forum.

How to Add User Roles in Wordpress

Goto editor and then in functions.php and add the following script code snippet.
function tp_add_role() {
    add_role( 'Guestblogger', 'Guestblogger',
             array(
                  'read',
                  'edit_posts',
                  'delete_posts',
                  'manage_categories',
                  'upload_files',
                  )
    );
}
add_action( 'init', 'tp_add_role' );
You're done.
add_role() is located in wp-includes/capabilities.php

How to Remove User Roles in Word-press

The following PHP script is to remove all the default user roles in WordPress. You can remove a few of them by removing the line you want to retain.
function remove_role() {
remove_role( 'editor' );
remove_role( 'author' );
remove_role( 'contributor' );
remove_role( 'subscriber' );
}
add_action( 'init', 'remove_role' );

 If this Article helped you please consider commenting your thoughts on it.

Hi There, I’m Vashishtha! Just another tech-savvy blogger on the plannet with a super power of troubleshooting.


EmoticonEmoticon