You are currently viewing How to add a new user role in wordpress

How to add a new user role in wordpress

In WordPress, users can have different roles, such as Administrator, Editor, Author, Contributor, and Subscriber. Each role has a different set of permissions and capabilities that determine what the user can or cannot do within the WordPress site. If you need a new role with specific permissions that are not covered by the existing roles, you can add a custom role using PHP code. Here are the steps in detail: Step 1: Open your theme’s functions.php file or create a new plugin file. The functions.php file is a core file in the WordPress theme that contains PHP code for adding functionality to the theme. If you are creating a custom plugin, you can create a new file in the wp- content/plugins/ directory. Step 2: Define a new custom role using the add_role() function. The add_role() function takes three arguments: Role slug: A unique identifier for the new role. It should be a lowercase string without spaces. Role name: The name of the role that will be displayed in the WordPress dashboard. Capabilities: An array of capabilities that define what the user can or cannot do. Here’s an example of how to define a new custom role called “My New Role” with read, edit_posts, and delete_posts capabilities: add_role( ‘my_new_role’, ‘My New Role’, array( ‘read’ => true, ‘edit_posts’ => true, ‘delete_posts’ => true, ) ); The read, edit_posts, and delete_posts capabilities are common capabilities that allow the user to read, edit, and delete their own posts. You can customize the capabilities and add or remove them as needed. Step 3: Customize the capabilities and permissions for the new role. To customize the capabilities and permissions for the new role, you need to modify the array of capabilities in the add_role() function. Here’s an example of how to add more capabilities to the “My New Role” role: add_role( ‘my_new_role’, ‘My New Role’, array( ‘read’ => true, ‘edit_posts’ => true, ‘delete_posts’ => true, ‘publish_posts’ => true, ‘edit_others_posts’ => true, ‘delete_others_posts’ => true, ) ); In this example, we’ve added the publish_posts, edit_others_posts, and delete_others_posts capabilities, which allow the user to publish their own posts, edit other users’ posts, and delete other users’ posts. You can customize the capabilities based on your specific needs. To see a list of all available capabilities, check out the WordPress Codex. Step 4: Save the file and refresh your WordPress dashboard. After you’ve defined the new custom role and customized the capabilities, save the file and refresh your WordPress dashboard to see the new role in the Users > Add New screen. You can assign the new role to users just like any other role. That’s it! You’ve successfully added a new user role in WordPress using PHP code.

How to add a new user role in wordpress

Leave a Reply