Usage

Core patterns and workflows for WPZylos WP-CLI.

Vendor Publish (Laravel-Style)

Publish package configuration files like Laravel's php artisan vendor:publish:

Publish by Provider

# Publish requirements config
wp myplugin vendor:publish --provider=RequirementsServiceProvider

# Force overwrite existing files
wp myplugin vendor:publish --provider=RequirementsServiceProvider --force

Publish All

# Publish all publishable assets
wp myplugin vendor:publish --all

List Publishable Assets

# See what can be published
wp myplugin vendor:publish:list

Output:

Publishable assets:

  [RequirementsServiceProvider]
    Source: /path/to/stubs/requirements.php.stub
    Target: config/requirements.php

Making Your Package Publishable

Add a static publishes() method to your service provider:

class MyServiceProvider extends ServiceProvider
{
    public static function publishes(): array
    {
        return [
            __DIR__ . '/../stubs/config.php.stub' => 'config/mypackage.php',
            __DIR__ . '/../stubs/views/' => 'resources/views/vendor/mypackage/',
        ];
    }
}

Creating Commands

Basic Command

use WP_CLI;

class GreetCommand
{
    public function run(array $args, array $assocArgs): void
    {
        $name = $args[0] ?? 'World';

        WP_CLI::success("Hello, {$name}!");
    }
}

Register Command

// In a service provider
public function register(ApplicationInterface $app): void
{
    parent::register($app);

    if (defined('WP_CLI') && WP_CLI) {
        $greet = new GreetCommand();
        WP_CLI::add_command($app->context()->slug() . ' greet', [$greet, 'run']);
    }
}

Usage

wp myplugin greet John
# Success: Hello, John!

Command Options

Arguments and Options

use WP_CLI;

class CreateUserCommand
{
    /**
     * Create a new user.
     *
     * ## OPTIONS
     *
     * <email>
     * : User email address
     *
     * [--role=<role>]
     * : User role
     * ---
     * default: subscriber
     * ---
     *
     * [--silent]
     * : Suppress output
     */
    public function create(array $args, array $assocArgs): void
    {
        $email = $args[0];
        $role = $assocArgs['role'] ?? 'subscriber';
        $silent = isset($assocArgs['silent']);

        // Create user...

        if (!$silent) {
            WP_CLI::success("User created: {$email}");
        }
    }
}

Usage

wp myplugin user create [email protected] --role=editor
wp myplugin user create [email protected] --silent

Output Methods

// Success message (green)
WP_CLI::success('Operation completed');

// Error message (red) - stops execution
WP_CLI::error('Something went wrong');

// Warning message (yellow)
WP_CLI::warning('Proceed with caution');

// Info message
WP_CLI::log('Processing...');

// Line output
WP_CLI::line('Plain text output');

Progress Bar

public function process(array $args, array $assocArgs): void
{
    $items = $this->getItems();

    $progress = \WP_CLI\Utils\make_progress_bar('Processing items', count($items));

    foreach ($items as $item) {
        $this->processItem($item);
        $progress->tick();
    }

    $progress->finish();
    WP_CLI::success('All items processed');
}

Confirmation

public function delete(array $args, array $assocArgs): void
{
    WP_CLI::confirm('Are you sure you want to delete all items?');

    // Proceed with deletion
    WP_CLI::success('Items deleted.');
}