Skip to content

Commit c7f03e4

Browse files
Copilotswissspidy
andauthored
Add guidance on where to place custom WP-CLI commands (#631)
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent a80feed commit c7f03e4

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

guides/commands-cookbook.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,9 +570,34 @@ Functional tests typically follow this pattern:
570570

571571
Convinced? Head on over to [wp-cli/scaffold-package-command](https://github.com/wp-cli/scaffold-package-command) to get started.
572572

573+
## Adding commands globally
574+
575+
If you want to have custom commands available globally, without needing to create a full plugin or package, you can use WP-CLI's `require` configuration option.
576+
577+
Edit your [global config file](https://make.wordpress.org/cli/handbook/references/config/) (usually found at `~/.wp-cli/config.yml`) to require a PHP file:
578+
579+
```yaml
580+
require:
581+
- ~/.wp-cli/commands.php
582+
```
583+
584+
Then create the file and add your custom commands:
585+
586+
```php
587+
<?php
588+
589+
WP_CLI::add_command( 'hello-world', function () {
590+
WP_CLI::success( "Hello World!" );
591+
} );
592+
```
593+
594+
Any commands registered in that file will be globally available whenever you run WP-CLI. This is great for personal helper commands that you want available across all your WordPress projects.
595+
596+
You can also pass the `--require=<path>` flag on the command line, or set the `WP_CLI_REQUIRE` environment variable, to load a custom PHP file for a single invocation.
597+
573598
## Distribution
574599

575-
Now that you've produce a command you're proud of, it's time to share it with the world. There are two common ways of doing so.
600+
Now that you've produced a command you're proud of, it's time to share it with the world. There are two common ways of doing so.
576601

577602
### Include in a plugin or theme
578603

references/internal-api/wp-cli-add-command.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ WP_CLI::add_command( 'foo', $foo );
5151
```
5252

5353

54+
For guidance on where to place your custom commands (e.g. globally via `~/.wp-cli/config.yml`, in a plugin or theme, or as a standalone package), see the [Commands Cookbook](https://make.wordpress.org/cli/handbook/guides/commands-cookbook/).
55+
5456
*Internal API documentation is generated from the WP-CLI codebase on every release. To suggest improvements, please submit a pull request.*
5557

5658

0 commit comments

Comments
 (0)