To add a custom user role via code, you can use one of the following methods:
Add code to the functions.php file in your theme
Use a code snippet plugin
Create a custom plugin for this purpose.
Let’s look at an example of creating a custom ‘Author Pro’ role that has enhanced capabilities for comment moderation. This code can be added to the functions.php file in your theme or implemented via a code snippet plugin.
/* Create Author Pro User Role */
add_role(
'author_pro', // System name of the role.
__( 'Author Pro' ), // Display name of the role.
array(
'read' => true,
'delete_posts' => true,
'delete_published_posts' => true,
'edit_posts' => true,
'publish_posts' => true,
'edit_published_posts' => true,
'upload_files' => true,
'moderate_comments'=> true, // This user will be able to moderate the comments.
)
);
After adding this snippet to your functions.php file, you can create a new user and assign them the ‘Author Pro’ role in the WordPress dashboard. To learn more about the add-role function, read the developer documentation from WordPress.