Skip to content

Commit dd15ee9

Browse files
authored
Merge pull request #579 from wp-cli/copilot/update-cli-init-documentation
2 parents f43c1fc + 62110a0 commit dd15ee9

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

guides/commands-cookbook.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,21 @@ if ( defined( 'WP_CLI' ) && WP_CLI ) {
503503
require_once dirname( __FILE__ ) . '/inc/class-plugin-cli-command.php';
504504
}
505505
```
506+
507+
Alternatively, you can use the `cli_init` hook to register your commands. This hook is fired during the WP-CLI Runner startup process, providing a dedicated event for command registration without needing to check for the `WP_CLI` constant.
508+
509+
```
510+
/**
511+
* Register custom WP-CLI commands using the cli_init hook.
512+
*/
513+
function myplugin_register_cli_commands() {
514+
require_once dirname( __FILE__ ) . '/inc/class-plugin-cli-command.php';
515+
WP_CLI::add_command( 'myplugin', 'MyPlugin_CLI_Command' );
516+
}
517+
add_action( 'cli_init', 'myplugin_register_cli_commands' );
518+
```
519+
520+
Both approaches are valid. Use the `WP_CLI` constant check when you need to conditionally load code based on whether WP-CLI is present. Use the `cli_init` hook when you want to hook into a specific point in the WP-CLI initialization process.
506521

507522
### Distribute as a stand-alone command
508523

0 commit comments

Comments
 (0)