How to create new user role in wordpress
To create a new user role in WordPress, you can follow these steps:
Method 1: Using the WordPress Dashboard
- Log in to your WordPress dashboard.
- Click on Users in the left-hand menu.
- Click on Add New at the top of the page.
- Enter a name for the new role in the Role Name field.
- Click Add Role.
Method 2: Using the WordPress Code Editor
-
Log in to your WordPress dashboard.
-
Click on Appearance in the left-hand menu.
-
Click on Editor.
-
In the editor, navigate to the
wp-content/plugins/users-role-editor/users-role-editor.php
file (or create a new file with this name if it doesn't exist). -
Add the following code to the file:
<?php function add_new_role() { $role = new WP_Role('new_role_name'); $role->add_cap('read'); $role->add_cap('edit_posts'); // Add more capabilities as needed return $role; } add_action('init', 'add_new_role'); ?>
Replace
new_role_name
with the name you want to give to the new role. -
Save the file.
Method 3: Using a Plugin
There are several plugins available that allow you to create new user roles in WordPress, such as:
- User Role Editor: This plugin provides a user-friendly interface for creating and managing user roles.
- Role Manager: This plugin allows you to create and manage custom user roles.
To use one of these plugins, follow these steps:
- Install and activate the plugin.
- Go to the plugin's settings page (e.g. User Role Editor > Settings).
- Click on the Add New Role button.
- Enter a name for the new role and add the desired capabilities.
- Click Add Role.
Additional Tips
- To assign the new role to a user, go to the Users page and click on the Edit link next to the user's name. Then, select the new role from the Role dropdown menu.
- You can also use the
wp_insert_user
hook to assign the new role to a user when they are created. For example:function assign_new_role_to_user($user_id) { $user = new WP_User($user_id); $user->set_role('new_role_name'); } add_action('wp_insert_user', 'assign_new_role_to_user');