How to Create and Manage Custom User Roles in WordPress?

In WordPress, each user role, like Administrator, Editor, Author, or Subscriber, comes with a predefined set of capabilities. These determine what actions users can perform on your site.

However, sometimes you may need to go beyond these default roles. For example, you might want to create a role that can edit and publish posts but not delete them, or one that can upload files but not manage plugins.

In this post, we’ll walk through how to create your own custom user role in WordPress using a simple code snippet.

Solution: Create and Manage Custom User Roles in WordPress

This code creates a custom WordPress user role called Custom Manager. You can place this snippet in your theme’s functions.php file or inside a custom plugin.

function ts_create_custom_user_role() {
    if ( ! get_role( 'custom_manager' ) ) {
        add_role(
            'custom_manager', // Role slug
            'Custom Manager', // Display name
            array(
                'read'           => true,   // Can read content
                'edit_posts'     => true,   // Can edit posts
                'delete_posts'   => false,  // Cannot delete posts
                'publish_posts'  => true,   // Can publish posts
                'upload_files'   => true,   // Can upload media
            )
        );
    }
}
add_action( 'init', 'ts_remove_custom_user_role' );

Once your custom role is created, you can easily add or remove capabilities from it using WordPress functions.

$role = get_role( 'custom_manager' );

if ( $role ) {
    // Give permission to manage categories
    $role->add_cap( 'manage_categories' );

    // Remove permission to publish posts
    $role->remove_cap( 'publish_posts' );
}

Removing a Custom Role

If you ever want to delete the custom role, you can use the remove_role() function.

/**
 * Remove Custom Role
 */
function ts_remove_custom_user_role() {
    remove_role( 'custom_manager' );
}
add_action( 'init', 'ts_remove_custom_user_role' );