定制 highliuk/wordpress-command 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

highliuk/wordpress-command

最新稳定版本:1.2.1

Composer 安装命令:

composer require highliuk/wordpress-command

包简介

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

README 文档

README

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.

统计信息

  • 总下载量: 17
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-06-07

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固