concept-image / wp-roles
WordPress roles from a config file in an Acorn theme: the admin_client role, a single-write synchroniser (wp acorn roles:update), and protection against privilege escalation by non-administrator user managers.
Requires
- php: >=8.3
- concept-image/wp-hook-attributes: ^0.1
- roots/acorn: ^6.4
Requires (Dev)
- brain/monkey: ^2.6
- larastan/larastan: ^3.0
- laravel/pint: ^1.18
- orchestra/testbench: ^10.0|^11.0
- pestphp/pest: ^4.0|^5.0
- pestphp/pest-plugin-arch: ^4.0|^5.0
- szepeviktor/phpstan-wordpress: ^2.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
WordPress roles from a config file in an Acorn theme
This Acorn package:
- applies the theme's
config/roles.phpto the WordPress roles withwp acorn roles:update(deploy scripts) or a dashboard widget: one write of the roles option, existing roles updated in place, no role removed while users still hold it; - ships
admin_client, the client's administrator role (content, media, menus, users, SEO, translations, forms; no settings, plugins, themes or unfiltered HTML); - keeps user managers without
manage_optionsfrom reaching administrators or anyone with more capabilities; - tells administrators when
config/roles.phpchanged since the last sync.
Nothing writes roles during normal requests.
Requirements
- PHP 8.3+
- WordPress 6.5+ on Roots Bedrock, Roots Acorn 6.4+
Installation
composer require concept-image/wp-roles
wp acorn vendor:publish --tag=wp-roles-config # theme's config/roles.php
wp acorn roles:update --dry-run
wp acorn roles:update
The Acorn service provider is discovered automatically. concept-image/wp-starter-theme already requires the
package. Add wp acorn roles:update to the deploy script.
Configuration
config/roles.php:
| Key | Default (package) | Published config |
|---|---|---|
roles_to_add | admin_client | admin_client |
roles_to_remove | [] | contributor, editor, author, subscriber, wpseo_manager, wpseo_editor |
default_role | null | null |
hide_administrators | true | true |
Acorn merges the theme's file with the package defaults at the top level only: a key the theme leaves out takes the package value. That's why the package removes no role by default; the published file holds the usual list.
'roles_to_add' => [
'admin_client' => [
'display_name' => 'Admin Client',
'capabilities' => [
'posts_capabilities' => ['edit_posts' => true, 'publish_posts' => true], // grouped…
'wpseo_manage_options' => true, // …or flat
],
],
// Only sets these capabilities on administrator; its other capabilities stay.
'administrator' => ['capabilities' => ['gravityforms_edit_settings' => true]],
],
'roles_to_remove' => ['editor', 'author'],
'default_role' => null,
'hide_administrators' => true,
- A managed role gets exactly its listed capabilities (
trueorfalse). Roles the config doesn't mention (plugins') are left alone. admin_clientkeeps its current label on a sync (the client's name for it); change it in the widget or with--label. Its slug never changes.default_role: when a sync removes the role of thedefault_roleoption (new user role), the option becomessubscriberif it still exists, else this role (a low-privilege role, checked), else''(no role) with a warning.hide_administrators: users withoutmanage_optionsdon't see administrators in the users list (wp-admin and REST). Cosmetic; the protection below doesn't depend on it.- An invalid file fails with a message naming the key (command, widget and admin notice), never a PHP error.
Applying the config
wp acorn roles:update --dry-run # show the changes, write nothing
wp acorn roles:update # apply
wp acorn roles:update --reassign=admin_client # move the users of removed roles first
wp acorn roles:update --label="Admin Acme" # rename admin_client
wp acorn roles:update --network # each site of a multisite network
A removed role still held by users stops the sync (nothing is written) until --reassign=<role> is given. Plugins
recreate their own roles when updated (Yoast SEO's wpseo_*): the next sync removes them again. Recreate a
deleted core role with wp role reset <role> (--all for every core role).
The dashboard widget "Rôles utilisateurs" (administrators with promote_users) lists the roles, renames
admin_client and runs the same sync. After a sync, the config hash is stored in the wp_roles_config_hash
option; when the file changes, administrators see a notice until the next sync.
Security model
admin_client manages users (create_users, edit_users, delete_users, promote_users, remove_users). On
a single site, WordPress lets such a user edit, delete or promote any user, administrators included, and give
any role. wp-roles closes that, always, on every screen and in the REST API:
map_meta_cap: editing, deleting, removing or changing the role of a user is denied when that user is an administrator, a super admin, or holds any capability the acting user lacks.editable_roles: the roles offered or accepted (user-new.php, user-edit.php, users.php bulk "Change role", RESTroles) are those whose capabilities the acting user holds, never administrator.- Users with
manage_optionsand super admins are never restricted.
Keep manage_options, unfiltered_html and plugin/theme/core management off non-administrator roles (the sync
warns), and never rely on hiding a menu to protect something.
Multisite
Roles are stored per site. roles:update --network syncs every site; without it, the current site (--url=).
The widget, the notice and the hash are per site. Super admins are never restricted and never manageable by site
users.
Claude Code
wp acorn vendor:publish --tag=wp-roles-ai copies the wp-roles skill and rules to the Bedrock root's
.claude/ (wp-roles-skill and wp-roles-rules separately); wp-starter-theme's starter:ai does it for all
packages.
Strings
Admin strings are French, in the wp-roles text domain (translate them with a wp-roles translation file or the
gettext filter). Command output is English.
Changelog
Please refer to CHANGELOG for more information. Upgrading from 2.x: UPGRADE.
Contributing
Please refer to CONTRIBUTING for more information.
Credits
- Banner icon: Admin ui icons created by Magnific - Flaticon
License
Please refer to LICENSE for more information.