WordPress is one of the best and flexible content management system.In WordPress, You can create new user roles, which have specific access to various parts of the admin. after that, you can create users and associate a user to the role you created.
This tutorial will walk you through setting up new user roles in WordPress and allowing them the necessary access to features within the WordPress content management system.
See about: Share user Data in multiple WordPress Installation
Ok, we’ll start by dive into the codes in which you need to define global variable $wp_roles
then after adding new roles you want to define for user using inbuilt function of WordPress add_role.
so Let’s see the code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | function init_add_roles() { global $wp_roles; if (class_exists('WP_Roles')) if ( ! isset( $wp_roles ) ) $wp_roles = new WP_Roles(); $wp_roles->add_role( 'role_name', __('Role Name'), array( 'read' => true, // True allows read capability, false to removes it. 'edit_posts' => false, 'delete_posts' => false, 'edit_published_posts' => true, 'publish_posts' => true, 'edit_files' => true, 'upload_files' => true ) ); } add_action('init', 'init_add_roles'); |
Here, I have added a new action for adding a new role for the user. same like that you can remove user role using the inbuilt function of WordPress remove_role.
Also Like: To Get number of comments of a post
1 2 3 4 | global $wp_roles; $wp_roles->remove_role( 'salesman'); |
Users and user roles are necessary parts of setting up any site that is going to be managed by more than one person.It’s pretty simple and easy, so don’t bother to use it in your development.
Thank you for reading.Feel free to share your thoughts! Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.