Skip to content
  1. Blog

Announcing Acorn User Roles

Ben Word Ben Word

WordPress’s native approach to managing user roles involves scattered add_role() and remove_role() calls that are easy to lose track of and difficult to keep consistent across environments. Role changes persist in the database, making it hard to know what the current state should be just by reading your code.

Introducing Acorn User Roles — a new package that allows you to manage user roles through a straightforward PHP config file.

Usage

$ composer require roots/acorn-user-roles

Once installed, you can publish the configuration file:

$ wp acorn vendor:publish --provider="Roots\AcornUserRoles\AcornUserRolesServiceProvider"

User roles are configured using a simple array structure in config/user-roles.php:

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | User Roles
    |--------------------------------------------------------------------------
    |
    | Define user roles to add or remove. Each key is the role slug.
    |
    | Set a role to `false` to remove it. Otherwise, provide an array with
    | `display_name` and `capabilities` to add or update the role.
    |
    | Capabilities can be a simple list (['read', 'edit_posts']) or an
    | associative array (['read' => true, 'edit_posts' => false]).
    |
    | Config is authoritative — existing roles will be synced to match,
    | and unlisted capabilities will be removed.
    |
    */

    'subscriber' => false,

    'librarian' => [
        'display_name' => 'Librarian',
        'capabilities' => ['read', 'edit_books', 'publish_books'],
    ],
];

Acorn User Roles will automatically register, update, or remove your user roles on every request — keeping your environments convergent and your config the single source of truth.

Discuss this post on Roots Discourse

About the author

Ben Word

Ben Word has been creating WordPress sites since 2004. He loves dogs, climbing, and yoga, and is passionate about helping people build awesome things on the web.

Subscribe for updates

Join over 8,000 subscribers on our newsletter to get the latest Roots updates and tips on building better WordPress sites

Looking for WordPress plugin recommendations, the newest modern WordPress projects, and general web development tips and articles?

One last step! Check your email for a verification link.