Skip to content

HighLiuk/wordpress-command

Repository files navigation

WordPress Command

An easy way to define custom commands in WordPress powered by Symfony Console instead of WP-CLI.

Installation

Use composer to install the package:

composer require highliuk/wordpress-command

Usage

First, create your custom command by extending the Command class:

use Highliuk\WordPressCommand\Command;

/**
 * Greets the blog with its name.
 */
class HelloBlog extends Command
{
    protected function handle(): void
    {
        $name = get_bloginfo('name');

        $this->line("Hello, $name!");
    }
}

Then, register your command in your WordPress code:

use Highliuk\WordPressCommand\Application;

$app = Application::getInstance();
$app->add(new HelloBlog());

Now you can run your custom command:

vendor/bin/console hello:blog
# Hello, My Blog!

You have access to all of the Symfony Console features, such as options and arguments. See the Symfony Console documentation for more information.

Features

Auto Inference for Name and Description

By default, the command name is inferred from the class name. For instance, the HelloBlog command will be available as hello:blog. Similarly, the command description is inferred from the class docblock. If you want to customize the command name and description, you can use the setName and setDescription methods in the configure method (see Customization), or you can use the shorthand properties:

use Highliuk\WordPressCommand\Command;

class HelloBlog extends Command
{
    protected $name = 'greet:blog';
    protected $description = 'Greets the blog with its name.';

    protected function handle(): void
    {
        $name = get_bloginfo('name');

        $this->line("Hello, $name!");
    }
}

Customization

You can customize the command by overriding the configure method (as for Symfony Console commands):

use Highliuk\WordPressCommand\Command;

class HelloBlog extends Command
{
    protected function configure(): void
    {
        $this->setName('greet:blog');
    }

    protected function handle(): void
    {
        $name = get_bloginfo('name');

        $this->line("Hello, $name!");
    }
}

Argument and Option Bindings

You can access arguments and options from your handle method:

use Highliuk\WordPressCommand\Command;
use Symfony\Component\Console\Input\InputArgument;

class GreetUser extends Command
{
    protected function configure(): void
    {
        $this
            ->addArgument('user', InputArgument::REQUIRED, 'The user to greet')
            ->addOption('uppercase', 'u', 'Whether to uppercase the user name');
    }

    protected function handle(string $user, bool $uppercase): void
    {
        if ($uppercase) {
            $user = strtoupper($user);
        }

        $this->line("Hello, $user!");
    }
}
vendor/bin/console greet:user john -u
# Hello, JOHN!

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages