PHPackages                             rockschtar/wordpress-role - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. rockschtar/wordpress-role

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

rockschtar/wordpress-role
=========================

WordPress Role Abstraction

1.3.0(2mo ago)09.5k↓45.9%MITPHPPHP &gt;=8.3CI passing

Since Jul 8Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/rockschtar/wordpress-role)[ Packagist](https://packagist.org/packages/rockschtar/wordpress-role)[ RSS](/packages/rockschtar-wordpress-role/feed)WikiDiscussions main Synced yesterday

READMEChangelog (1)Dependencies (6)Versions (13)Used By (0)

rockschtar/wordpress-role
=========================

[](#rockschtarwordpress-role)

[![CI](https://github.com/rockschtar/wordpress-role/actions/workflows/ci.yml/badge.svg)](https://github.com/rockschtar/wordpress-role/actions/workflows/ci.yml)

A PHP library that lets you define WordPress roles as typed classes. Instead of scattering `add_role()` / `remove_role()` calls across your plugin, each role becomes a self-contained class that knows its own name, display label, and capabilities. Register and unregister it with a single static call.

Designed for composer-based WordPress projects such as [roots/bedrock](https://github.com/roots/bedrock) or [johnpbloch/wordpress](https://github.com/johnpbloch/wordpress).

Requirements
------------

[](#requirements)

- PHP 8.3
- [Composer](https://getcomposer.org/)

Installation
------------

[](#installation)

```
composer require rockschtar/wordpress-role
```

How to
------

[](#how-to)

### 1. Define a role

[](#1-define-a-role)

Extend `Role` and implement the three required methods. All methods must be `static`.

```
use Rockschtar\WordPress\Role\Role;

class FAQManagerRole extends Role
{
    public static function roleName(): string
    {
        return 'faq_manager';
    }

    public static function displayName(): string
    {
        return __('FAQ Manager', 'my-textdomain');
    }

    public static function capabilities(): array
    {
        return [
            'edit_faq',
            'read_faq',
            'delete_faq',
        ];
    }
}
```

By default the role inherits all capabilities from the built-in `subscriber` role. Override `inheritFrom()` to use a different base:

```
protected function inheritFrom(): string
{
    return 'editor';
}
```

### 2. Register and unregister

[](#2-register-and-unregister)

Wire the role to your plugin's activation and deactivation hooks:

```
register_activation_hook(MY_PLUGIN_FILE, [FAQManagerRole::class, 'register']);
register_deactivation_hook(MY_PLUGIN_FILE, [FAQManagerRole::class, 'unregister']);
```

When your plugin defines several roles, group them together:

```
const ROLES = [
    FAQManagerRole::class,
    EditorPlusRole::class,
];

register_activation_hook(MY_PLUGIN_FILE, function () {
    foreach (ROLES as $role) {
        $role::register();
    }
});

register_deactivation_hook(MY_PLUGIN_FILE, function () {
    foreach (ROLES as $role) {
        $role::unregister();
    }
});
```

Examples
--------

[](#examples)

**Check if the current user has the role:**

```
if (current_user_can(FAQManagerRole::roleName())) {
    // ...
}
```

**Assign the role to a user**, e.g. after registration:

```
$user->set_role(FAQManagerRole::roleName());
```

**Use the role in a REST API permission callback:**

```
register_rest_route('my-plugin/v1', '/faqs', [
    'methods'             => 'GET',
    'callback'            => 'my_plugin_get_faqs',
    'permission_callback' => fn() => current_user_can(FAQManagerRole::roleName()),
]);
```

Available hooks and filters
---------------------------

[](#available-hooks-and-filters)

HookTypeDescription`rswpr_before_register_role`actionFires before a role is registered. Receives the `Role` instance.`rswpr_after_register_role`actionFires after a role is registered. Receives the `Role` instance.`rswpr_before_unregister_role`actionFires before a role is removed. Receives the `Role` instance.`rswpr_after_unregister_role`actionFires after a role is removed. Receives the `Role` instance.`rswpr_default_inherit_from_role`filterOverride the default inherited role (`subscriber`).`rswp_get_wp_role`filterFilter the `WP_Role` object returned by `getWPRole()`.License
-------

[](#license)

rockschtar/wordpress-role is open source and released under the MIT license. See [LICENSE](LICENSE) for details.

###  Health Score

53

—

FairBetter than 96% of packages

Maintenance86

Actively maintained with recent releases

Popularity24

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity78

Established project with proven stability

 Bus Factor1

Top contributor holds 88.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~264 days

Recently: every ~458 days

Total

9

Last Release

73d ago

PHP version history (2 changes)1.0.0-alpha.1PHP &gt;=7.1

1.2.0PHP &gt;=8.3

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/87191?v=4)[Stefan Helmer](/maintainers/rockschtar)[@rockschtar](https://github.com/rockschtar)

---

Top Contributors

[![rockschtar](https://avatars.githubusercontent.com/u/87191?v=4)](https://github.com/rockschtar "rockschtar (31 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (4 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/rockschtar-wordpress-role/health.svg)

```
[![Health](https://phpackages.com/badges/rockschtar-wordpress-role/health.svg)](https://phpackages.com/packages/rockschtar-wordpress-role)
```

###  Alternatives

[kartik-v/yii2-password

Useful password strength validation utilities for Yii Framework 2.0

761.3M17](/packages/kartik-v-yii2-password)[vitalybaev/laravel5-dkim

Laravel 5/6 package for signing outgoing messages with DKIM.

3163.1k](/packages/vitalybaev-laravel5-dkim)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
