Skip to content

Add guidance on where to place custom WP-CLI commands#631

Merged
swissspidy merged 2 commits intomainfrom
copilot/improve-documentation-customizations
Mar 17, 2026
Merged

Add guidance on where to place custom WP-CLI commands#631
swissspidy merged 2 commits intomainfrom
copilot/improve-documentation-customizations

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 17, 2026

Users had no clear documentation on where to register personal/global WP-CLI customizations — the WP_CLI::add_command() reference page showed how to register commands but not where to put the code.

Changes

  • guides/commands-cookbook.md — Added "Adding commands globally" section covering the require approach in ~/.wp-cli/config.yml:

    # ~/.wp-cli/config.yml
    require:
      - ~/.wp-cli/commands.php
    <?php
    WP_CLI::add_command( 'hello-world', function () {
        WP_CLI::success( "Hello World!" );
    } );

    Also mentions --require=<path> CLI flag and WP_CLI_REQUIRE env var for per-invocation use. Fixed pre-existing typo ("produce" → "produced") in the Distribution section.

  • references/internal-api/wp-cli-add-command.md — Added a forward reference to the Commands Cookbook so readers landing on the API page are directed to placement guidance.

Original prompt

This section details on the original issue you should resolve

<issue_title>Improve documentation: where to add customizations?</issue_title>
<issue_description>I'm ready to start building my custom wp-cli stuff, but I'm not sure where I should put my code. I found https://make.wordpress.org/cli/handbook/internal-api/wp-cli-add-command/ which is good, but not good enough.

It'll be very useful to point readers of this page of to a place where they learn more about where to actually add this example code. This could also be on the page itself.

I'll be happy to do a pull request for this if/when I figure it out on my own.</issue_description>

Comments on the Issue (you are @copilot in this section)

@schlessera @guaka If you want to have a single PHP file somewhere to just add arbitrary commands to, you can do the following:

Edit your global config file (usually found in ~/.wp-cli/config.yml) to require a PHP file in that same folder:

require:
  some-file.php

Then add said file (~/.wp-cli/some-file.php in this case) and add whatever commands you want to have globally added. Here's an example to get you started:

<?php

WP_CLI::add_command( 'hello-world', function () { echo "Hello World!\n"; } );

Any command you will add to that file will be globally available from now on.</comment_new>
<comment_new>@schlessera
Note: I'll accept PRs for adding the above to the documentation if it can be written in a clear and understandable way.</comment_new>


📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI changed the title [WIP] Improve documentation for adding custom wp-cli customizations docs: Add guidance on where to place custom WP-CLI commands Mar 17, 2026
Copilot AI requested a review from swissspidy March 17, 2026 16:43
@swissspidy swissspidy changed the title docs: Add guidance on where to place custom WP-CLI commands Add guidance on where to place custom WP-CLI commands Mar 17, 2026
@swissspidy swissspidy marked this pull request as ready for review March 17, 2026 17:06
@swissspidy swissspidy requested a review from a team as a code owner March 17, 2026 17:06
Copilot AI review requested due to automatic review settings March 17, 2026 17:06
@swissspidy swissspidy merged commit c7f03e4 into main Mar 17, 2026
8 checks passed
@swissspidy swissspidy deleted the copilot/improve-documentation-customizations branch March 17, 2026 17:07
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds missing “where should I put this code?” guidance for WP-CLI custom commands by documenting a global require approach in the Commands Cookbook and linking to it from the WP_CLI::add_command() reference page.

Changes:

  • Documented registering personal/global commands via require in ~/.wp-cli/config.yml, plus per-invocation alternatives (--require, WP_CLI_REQUIRE).
  • Added a cross-link from the WP_CLI::add_command() internal API reference page to the Commands Cookbook.
  • Fixed a small typo in the Commands Cookbook (“produce” → “produced”).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
guides/commands-cookbook.md Adds a new “Adding commands globally” section and corrects a typo in Distribution.
references/internal-api/wp-cli-add-command.md Adds a pointer to the Commands Cookbook for placement guidance.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +497 to +501
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:

```yaml
require:
- ~/.wp-cli/commands.php

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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve documentation: where to add customizations?

3 participants