Adding Custom User Roles in WordPress
Custom Code Solution
The following code creates a custom user role and should be placed in your theme's functions.php
file or in a site-specific plugin:
This code creates a new "Editor Plus" role with extended capabilities:
function create_editor_plus_role() {
$editor_plus_capabilities = get_role('editor')->capabilities;
$editor_plus_capabilities['edit_theme_options'] = true;
$editor_plus_capabilities['manage_categories'] = true;
add_role(
'editor_plus',
'Editor Plus',
$editor_plus_capabilities
);
}
add_action('init', 'create_editor_plus_role');
To remove the custom role when it's no longer needed:
function remove_editor_plus_role() {
remove_role('editor_plus');
}
// Uncomment next line to remove the role
// add_action('init', 'remove_editor_plus_role');
To add specific capabilities to an existing role:
function modify_role_capabilities() {
$role = get_role('author');
$role->add_cap('upload_files', true);
$role->add_cap('edit_published_posts', true);
}
add_action('init', 'modify_role_capabilities');
Common Capabilities Reference
-
edit_posts
-
publish_posts
-
edit_published_posts
-
delete_posts
-
upload_files
-
edit_pages
-
edit_theme_options
-
manage_categories
-
manage_options
Plugin Solutions
If you prefer using plugins, here are reliable options:
-
User Role Editor - Most popular solution with a visual interface
-
Members - Clean interface with role management features
-
Capability Manager Enhanced - Advanced role management with multisite support
Best Practices
- Always test new roles in a development environment first
- Back up your database before making role changes
- Use the principle of least privilege - give users only the capabilities they need
- Consider using a plugin for complex role structures
- Document any custom roles and their purposes for future reference
Important Notes
- Custom roles persist in the database even after deactivating themes/plugins
- Use
remove_role()
when cleaning up custom roles
- Changes to existing roles should be made with caution as they affect all users with that role
- Consider using a must-use plugin instead of
functions.php
for role management in production