To add a new user role in WordPress, you use the add_role() function. You can place this snippet in your active theme’s functions.php file or within a custom plugin.
Here is the standard snippet to create a custom role (in this example, a “Manager”)
/**
* Add a custom user role.
*/
function wp_add_custom_user_role() {
// Check if the role already exists to avoid unnecessary database writes
if ( ! get_role( 'manager' ) ) {
add_role(
'manager', // Internal role name (lowercase, no spaces)
__( 'Manager' ), // Display name for the WordPress admin area
array(
'read' => true, // Allows the user to read content
'edit_posts' => true, // Allows the user to edit their own posts
'upload_files' => true, // Allows the user to upload media
'delete_posts' => false, // Denies the user the ability to delete posts
)
);
}
}
// Hook into 'init' to execute the function
add_action( 'init', 'wp_add_custom_user_role' );