Search by

piotrpress / wordpress-abilities

PiotrPress

This is a lightweight PHP 8+ library for defining and registering custom WordPress Abilities using modern, attribute-based metadata.

Package info

github.com/PiotrPress/wordpress-abilities

pkg:composer/piotrpress/wordpress-abilities

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-08-31 09:08 UTC

This package is auto-updated.

Last update: 2026-08-31 09:10:13 UTC


README

This is a lightweight PHP 8+ library for defining and registering custom WordPress Abilities using modern, attribute-based metadata.

It helps you keep Ability definitions close to the code that executes them. By combining #[Ability(...)] attribute with AbilityInterface / AbstractAbility, you can describe data in a clear, typed way, then register everything with a single call.

Installation

composer require piotrpress/wordpress-abilities

Example

require_once __DIR__ . '/vendor/autoload.php';

use PiotrPress\WordPress\Ability;
use PiotrPress\WordPress\AbilityCategory;
use PiotrPress\WordPress\AbstractAbility;

#[ Ability(
    name: 'example/ability',
    label: 'Example Ability',
    description: 'Example description',
    category: new AbilityCategory(
        slug: 'example-category',
        label: 'Example Category',
        description: 'Example description'
    ),
    textdomain: 'example',
    public: true
) ]
final class ExampleAbility extends AbstractAbility {
    public function __invoke( array $input ) : mixed {...}
    public function input() : array {...}
    public function output() : array {...}
    public function permission( array $input ) : bool|\WP_Error {...}
}

( new ExampleAbility() )->register();

Category

The category argument in Ability attribute accepts two forms:

  • AbilityCategory object - use this when you want to register a new category. If a category with the same slug already exists, no re-registration is attempted
  • string - use this when you want to assign the Ability to an existing category slug.

Development

composer format

NOTE: The format command will format composer.json file, it's required PHP >= 8.2.

composer test

NOTE: The test command will run PHPUnit tests, it's required PHP >= 8.3.

Requirements

  • PHP >= 8.0

License

GPL3