Adds a role, if it does not exist.
Parameters
$role
stringrequired- Role name.
$display_name
stringrequired- Display name for role.
$capabilities
bool[]optional- List of capabilities keyed by the capability name, e.g. array(
'edit_posts'
=> true,'delete_posts'
=> false ).Default:
array()
Source
function add_role( $role, $display_name, $capabilities = array() ) {
if ( empty( $role ) ) {
return;
}
return wp_roles()->add_role( $role, $display_name, $capabilities );
}
Changelog
Version | Description |
---|---|
2.0.0 | Introduced. |
Be sure to use this function (and similar role functions) only in an activation hook or within a conditional block. There is no need for this to execute every time the page loads, and it will keep updating the database every time it’s called.
For example, this will store an option to track the version of the custom roles and will only update the database once:
You can also easily create a new user role based on an existing user role.
( equivalent to WP CLI:
wp role create <role-key> <role-name> --clone=<role>
)Example: create a role Superintended with the same capabilities as Administrator
A list of all possible Capabilities per user roles:
https://wordpress.org/support/article/roles-and-capabilities/#capability-vs-role-table
Super Admin
=========================
create_sites
delete_sites
manage_network
manage_sites
manage_network_users
manage_network_plugins
manage_network_themes
manage_network_options
upload_plugins
upload_themes
upgrade_network
setup_network
Super Admin + Administrator
==========================================
activate_plugins (single site or enabled by network setting)
create_users (single site)
delete_plugins (single site)
delete_themes (single site)
delete_users (single site)
edit_files (single site)
edit_plugins (single site)
edit_theme_options
edit_themes (single site)
edit_users (single site)
export
import
Super Admin + Administrator
=============================================
install_plugins (single site)
install_themes (single site)
list_users
manage_options
promote_users
remove_users
switch_themes
update_core (single site)
update_plugins (single site)
update_themes (single site)
edit_dashboard
customize
delete_site
Super Admin + Administrator + Editor
====================================================
moderate_comments
manage_categories
manage_links
edit_others_posts
edit_pages
edit_others_pages
edit_published_pages
publish_pages
delete_pages
delete_others_pages
delete_published_pages
delete_others_posts
delete_private_posts
edit_private_posts
read_private_posts
delete_private_pages
edit_private_pages
read_private_pages
unfiltered_html (single site)
unfiltered_html
Super Admin + Administrator + Editor + Author
==========================================================
edit_published_posts
upload_files
publish_posts
delete_published_posts
Super Admin + Administrator + Editor + Author + Contributor
========================================================================
edit_posts
delete_posts
Super Admin + Administrator + Editor + Author + Contributor + Subscriber
======================================================================================
read
Create a new role when a plugin is activated
See
register_activation_hook
.Example
Create a new “Guest Author” role.
Note: Delete existing role
You can not change the capabilities of an existing role using
add_role()
. This function will stop executing and returnnull
is the specified role name already exists.You can change a user role’s capabilities (or display name) by using
remove_role()
, thenadd_role()
.This is for development only. Once you have nailed down your list of capabilities, there’s no need to keep the remove_role() code.
Usage
Note: When to call
Make sure the global
$wp_roles
is available before attempting to add or modify a role. The best practice is to use a plugin (or theme) activation hook to make changes to roles (since you only want to do it once!).mu-plugins
loads too early, so use an action hook (like'init'
) to wrap youradd_role()
call if you’re doing this in the context of an mu-plugin.