GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - Pollora/wp-cli: WP-CLI commands declared with PHP attributes: a class with #[WpCli], subcommands with #[Command], synopsis, hooks and conditions as attributes. Runs with or without Laravel.

BranchesTags
Open more actions menu

Latest commit

 

History

58 Commits
58 Commits

Folders and files

NameName
Last commit message
Last commit date
.claude
.claude
 
 
.github/workflows
.github/workflows
 
 
src
src
 
 
tests
tests
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
.harness.json
.harness.json
 
 
AGENTS.md
AGENTS.md
 
 
CHANGELOG.md
CHANGELOG.md
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
api-surface.json
api-surface.json
 
 
composer-dependency-analyser.php
composer-dependency-analyser.php
 
 
composer.json
composer.json
 
 
phpstan.neon
phpstan.neon
 
 
phpunit.xml.dist
phpunit.xml.dist
 
 
pint.json
pint.json
 
 
rector.php
rector.php
 
 
renovate.json
renovate.json
 
 

Repository files navigation

Pollora WP-CLI: WP-CLI commands declared with PHP attributes

Latest version Total downloads Tests License

WP-CLI commands written as classes: #[WpCli] on a class registers it, its public methods become subcommands, and the command and subcommand names come from the class and method names unless you give them. Synopsis, when, before and after callbacks are attributes too; no WP_CLI::add_command() call to write.

Part of Pollora, the Laravel framework for WordPress, where it is installed and wired for you, with a pollora:make:wp-cli generator: see WP-CLI Commands. It runs without Laravel too, in a WordPress plugin, a Bedrock site or any PHP project.

Installation

composer require pollora/wp-cli

Requires PHP 8.3+. It builds on pollora/attributes and pollora/discovery. WP-CLI itself provides the WP_CLI classes when it runs.

Declaring commands

use Pollora\Attributes\WpCli;
use Pollora\Attributes\WpCli\Command;
use Pollora\Attributes\WpCli\Synopsis;

/**
 * Manage the reports.
 */
#[WpCli]
final class ReportsCommand
{
    public function __construct(private readonly ReportBuilder $reports) {}

    /**
     * Build a report.
     */
    #[Command('build')]
    #[Synopsis('<month> [--format=<format>]')]
    public function build(array $arguments, array $options): void
    {
        WP_CLI::success($this->reports->build($arguments[0], $options['format'] ?? 'csv'));
    }

    #[Command('rm')]
    public function deleteReport(array $arguments, array $options): void {}
}
wp reports build 2026-10 --format=json
wp reports rm 2026-09
  • The command name is the class name in kebab case, without its Command suffix: ReportsCommand → reports. #[WpCli('reports')] sets it. A class with __invoke() is a single command.
  • A method with #[Command] is a subcommand registered by this package: #[Command('rm')] names it, and its #[Synopsis], #[When], #[BeforeInvoke], #[AfterInvoke] and #[IsDeferred] become the matching WP_CLI::add_command() arguments.
  • WP-CLI also runs the class's other public methods as subcommands, under their method name, with their help read from the docblock.
  • The class is built once, with its constructor dependencies, and that instance runs every command and subcommand.

Wiring it outside Pollora

Run the discovery engine with WpCliDiscovery when WP-CLI loads your plugin:

use Pollora\Discovery\Domain\Models\DiscoveryLocation;
use Pollora\Discovery\Infrastructure\Services\DiscoveryEngine;
use Pollora\WpCli\Application\Services\WpCliService;
use Pollora\WpCli\Infrastructure\Adapters\WpCliAdapter;
use Pollora\WpCli\Infrastructure\Services\WpCliDiscovery;

if (defined('WP_CLI') && WP_CLI) {
    (new DiscoveryEngine)
        ->addLocation(new DiscoveryLocation('MyPlugin\\', __DIR__.'/src'))
        ->addDiscovery('wp_cli_commands', new WpCliDiscovery(new WpCliService(new WpCliAdapter)))
        ->run();
}

Commands are built by the engine's instance resolver: by reflection by default (constructor parameters typed with a class are built in turn), by your container if you give the engine a ContainerInstanceResolver (PSR-11). WpCliService and WpCliDiscovery take a PSR-3 logger.

Testing

composer test   # unit tests, PHPStan, Pint

License

MIT. See LICENSE.

About

WP-CLI commands declared with PHP attributes: a class with #[WpCli], subcommands with #[Command], synopsis, hooks and conditions as attributes. Runs with or without Laravel.

pollora.dev/advanced/wp-cli/

Topics

attributesphppollorawordpresswp-cli

Resources

Readme
MIT license

Code of conduct

Code of conduct

Contributing

Contributing

Security policy

Security policy
Activity
Custom properties

Stars

1 star

Watchers

0 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.