Trigger new 'cli_init' hook during WordPress plugins_loaded action.#4861
Trigger new 'cli_init' hook during WordPress plugins_loaded action.#4861schlessera merged 9 commits intowp-cli:masterfrom jmichaelward:issue-4818-add-wordpress-action-to-initialize-plugin-commands
Conversation
php/WP_CLI/Runner.php
Outdated
| } | ||
|
|
||
| public function start() { | ||
| WP_CLI::add_wp_hook( 'plugins_loaded', function () { |
There was a problem hiding this comment.
I'm still not 100% certain this is the best possible timing for the action.
Here are some alternatives I'd like to discuss:
- Adding the action to the bootstrap process controlled here: https://github.com/wp-cli/wp-cli/blob/master/php/bootstrap.php#L15-L34 . This would let us provide the hook as early as possible. A good fit might be between
RegisterFrameworkCommandsandIncludeFallbackAutoloader. - Add it after the
$this->load_wordpress()call also found within theRunner::start()method. This would make the Plugin API be available without fail whencli_initis being used.
Both might be inconsequential if people only ever use the hook within plugins. But maybe they want to use it in a drop-in, for example? (not sure that even makes sense)
What are your thoughts on this, @jmichaelward?
features/command.feature
Outdated
| core custom-subcommand | ||
| """ | ||
|
|
||
| Scenario: An activated plugin should successfully add custom commands when hooked on the cli_init action. |
There was a problem hiding this comment.
CS: No period after the scenario name.
features/command.feature
Outdated
| add_action( 'cli_init', function() { | ||
| require_once plugin_dir_path( __FILE__ ) . '/class-custom-command.php'; | ||
| $command = new Custom_Command(); | ||
| WP_CLI::add_command( 'custom', array( $command, 'do_custom_command' ) ); |
There was a problem hiding this comment.
If you are not actually running the command, there's no point in creating a command implementation. This is probably the shortest form of a command that you can verify on wp help:
WP_CLI::add_command( 'custom', function () {} );
This way, you can get rid of the class-custom-command.php file and everything related.
…to WordPress init hook.
php/WP_CLI/Runner.php
Outdated
| 4 | ||
| ); | ||
|
|
||
| // Set up hook for WP-CLI packages to use. |
There was a problem hiding this comment.
This is misleading, WP-CLI packages don't need this.
It should be something like:
// Set up hook for plugins & themes to conditionally add WP-CLI commands.
features/command.feature
Outdated
| Then STDOUT should contain: | ||
| """ | ||
| wp custom | ||
| """ No newline at end of file |
This PR will call do_action( 'cli_init' ) during the plugins_loaded event, which will allow developers to hook their custom WP-CLI commands into
WordPress without having to check for the definition and existence of the WP_CLI constant.
It includes one Behat test to validate that the action is fired.
Closes #4818.