Add custom user roles and capabilities to WordPress programmatically
A WordPress role is a named set of capabilities, stored in the wp_user_roles option. A user can have several roles, and code checks the capabilities, not the roles: current_user_can( 'edit_posts' ). Custom roles and capabilities let you give exactly the rights a job needs, instead of making everybody an administrator.
Problem: The site needs an “Editor Plus” role that can publish posts and manage WooCommerce orders, but cannot install plugins, change themes or manage users. The Editor role has no order management, and the Shop Manager role has too much.
Solution: Create the role once, on plugin activation, with add_role(), using the capabilities of Editor as the base plus the order capabilities. Check custom capabilities with current_user_can() and grant dynamic ones with the user_has_cap filter.
Create the role
<?php
register_activation_hook( __FILE__, 'myplugin_add_editor_plus_role' );
function myplugin_add_editor_plus_role() {
$editor = get_role( 'editor' );
$editor_caps = $editor ? $editor->capabilities : array();
$order_caps = array(
'read_shop_order' => true,
'edit_shop_order' => true,
'edit_shop_orders' => true,
'edit_others_shop_orders' => true,
'publish_shop_orders' => true,
'read_private_shop_orders'=> true,
);
remove_role( 'editor_plus' ); // so a re-activation applies changed capabilities
add_role( 'editor_plus', __( 'Editor Plus', 'myplugin' ), array_merge( $editor_caps, $order_caps ) );
}
add_role() writes to the database, so it runs on activation, not on every init. If the role already exists, add_role() does nothing, which is why the old version is removed first.
A custom capability for existing roles
<?php
register_activation_hook( __FILE__, function () {
foreach ( array( 'administrator', 'editor_plus' ) as $role_name ) {
$role = get_role( $role_name );
if ( $role ) {
$role->add_cap( 'view_order_reports' );
}
}
} );
// A page that requires the capability
add_action( 'admin_menu', function () {
add_submenu_page(
'woocommerce',
__( 'Order Reports', 'myplugin' ),
__( 'Order Reports', 'myplugin' ),
'view_order_reports',
'myplugin-order-reports',
'myplugin_order_reports_page'
);
} );
Capabilities that depend on data
For rights that depend on user meta or a subscription, do not change the stored roles; grant the capability at runtime:
<?php
add_filter( 'user_has_cap', function ( $allcaps, $caps, $args, $user ) {
if ( in_array( 'access_premium_content', $caps, true ) && get_user_meta( $user->ID, 'is_premium', true ) ) {
$allcaps['access_premium_content'] = true;
}
return $allcaps;
}, 10, 4 );
// in a template
if ( current_user_can( 'access_premium_content' ) ) {
// premium part
}
Meta capabilities like edit_post for one specific post are translated into primitive ones (edit_posts, edit_others_posts) by the map_meta_cap filter; a custom post type with its own capability_type and 'map_meta_cap' => true gets this mapping automatically.
Removing the role
<?php
register_uninstall_hook( __FILE__, 'myplugin_remove_editor_plus_role' );
function myplugin_remove_editor_plus_role() {
foreach ( get_users( array( 'role' => 'editor_plus', 'fields' => 'ID' ) ) as $user_id ) {
( new WP_User( $user_id ) )->set_role( 'editor' );
}
remove_role( 'editor_plus' );
}
Remove the role on uninstall, not on deactivation, and move its users to another role first: a user whose only role disappears has no rights at all and cannot even see the dashboard.
NOTE: Roles live in the database, not in the code, so keep their definition in a plugin, not in the theme. A nonce never replaces a capability check: for REST endpoints, admin pages and AJAX handlers the decision is always current_user_can(), see custom REST endpoints with permission callbacks.