WordPress WP-CLI Internal API
Use this skill when you are writing WP-CLI itself, not merely operating WordPress through wp.
The official internal API reference exposes a stable surface for command registration, logging, hooks, execution, and output formatting. This skill keeps that stable surface close at hand and gives you a quick command skeleton generator.
Use This Skill For
- - building custom WP-CLI commands
- reviewing command classes that call INLINECODE1
- choosing between
WP_CLI::log(), success(), warning(), and INLINECODE5 - formatting output with INLINECODE6
- running one WP-CLI command from another with INLINECODE7
- using hooks such as
WP_CLI::add_hook() and INLINECODE9
Do Not Use This Skill For
- - normal site administration where a stock WP-CLI command already exists
- REST route implementation or review
- plugin business logic that has nothing to do with WP-CLI command entrypoints
Workflow
1. Stay On The Stable Surface
Read references/stable-internal-api.md.
Start from the officially documented stable methods instead of undocumented internals.
2. Render A Command Skeleton
Use:
CODEBLOCK0
This gives you a minimal command class that already uses:
- - INLINECODE10
- INLINECODE11
- INLINECODE12
- INLINECODE13
3. Choose Output And Error Patterns Deliberately
Read references/command-patterns.md.
Default rule:
- - human progress:
log() or INLINECODE15 - successful completion: INLINECODE16
- non-fatal concern: INLINECODE17
- fatal stop: INLINECODE18
4. Prefer Internal Execution Helpers Over Shelling Out
If a command needs to trigger another WP-CLI command, prefer:
- - INLINECODE19
- INLINECODE20
Reach for generic shell execution only when the task is truly outside WP-CLI.
Files
- -
scripts/render-command-skeleton.sh: render a minimal custom command class and registration stub - INLINECODE22 : grouped stable API surface from the official WP-CLI internal API docs
- INLINECODE23 : practical command-authoring patterns and guardrails
WordPress WP-CLI 内部 API
当您正在编写 WP-CLI 本身,而不仅仅是通过 wp 操作 WordPress 时,请使用此技能。
官方内部 API 参考为命令注册、日志记录、钩子、执行和输出格式化提供了稳定的接口。此技能让您随时掌握该稳定接口,并为您提供快速命令骨架生成器。
此技能适用于
- - 构建自定义 WP-CLI 命令
- 审查调用 WPCLI::addcommand() 的命令类
- 在 WPCLI::log()、success()、warning() 和 error() 之间进行选择
- 使用 WPCLI\Utils\formatitems() 格式化输出
- 使用 WPCLI::runcommand() 从另一个 WP-CLI 命令运行一个命令
- 使用 WPCLI::addhook() 和 WPCLI::dohook() 等钩子
此技能不适用于
- - 已有标准 WP-CLI 命令的常规站点管理
- REST 路由实现或审查
- 与 WP-CLI 命令入口点无关的插件业务逻辑
工作流程
1. 坚持使用稳定接口
阅读 references/stable-internal-api.md。
从官方文档记录的稳定方法开始,而不是未文档化的内部方法。
2. 生成命令骨架
使用:
bash
scripts/render-command-skeleton.sh --command acme report --class AcmeReportCommand
scripts/render-command-skeleton.sh --command acme report --class AcmeReportCommand --write /tmp/report-command.php
这将为您提供一个已使用以下内容的最小命令类:
- - WPCLI::addcommand()
- WPCLI::success()
- WPCLI\Utils\getflagvalue()
- WPCLI\Utils\formatitems()
3. 慎重选择输出和错误模式
阅读 references/command-patterns.md。
默认规则:
- - 人类可读的进度:log() 或 line()
- 成功完成:success()
- 非致命问题:warning()
- 致命停止:error()
4. 优先使用内部执行辅助函数而非外部调用
如果命令需要触发另一个 WP-CLI 命令,优先使用:
- - WPCLI::runcommand()
- WPCLI::launch_self()
只有当任务确实在 WP-CLI 之外时,才考虑使用通用 shell 执行。
文件
- - scripts/render-command-skeleton.sh:生成最小自定义命令类和注册存根
- references/stable-internal-api.md:来自官方 WP-CLI 内部 API 文档的分组稳定 API 接口
- references/command-patterns.md:实用的命令编写模式和防护措施