Skip to content

Commit c988a2c

Browse files
authored
Merge pull request #4861 from jmichaelward/issue-4818-add-wordpress-action-to-initialize-plugin-commands
Trigger new 'cli_init' hook during WordPress plugins_loaded action.
2 parents 108d0f3 + a2df46c commit c988a2c

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

features/command.feature

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,3 +1666,21 @@ Feature: WP-CLI Commands
16661666
"""
16671667
core custom-subcommand
16681668
"""
1669+
1670+
Scenario: An activated plugin should successfully add custom commands when hooked on the cli_init action
1671+
Given a WP installation
1672+
And a wp-content/plugins/custom-command/custom-cmd.php file:
1673+
"""
1674+
<?php
1675+
// Plugin Name: Custom Command
1676+
1677+
add_action( 'cli_init', function() {
1678+
WP_CLI::add_command( 'custom', function () {} );
1679+
} );
1680+
"""
1681+
And I run `wp plugin activate custom-command`
1682+
When I run `wp custom --help`
1683+
Then STDOUT should contain:
1684+
"""
1685+
wp custom
1686+
"""

php/WP_CLI/Runner.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,6 @@ private function set_alias( $alias ) {
966966
}
967967

968968
public function start() {
969-
970969
// Enable PHP error reporting to stderr if testing. Will need to be re-enabled after WP loads.
971970
if ( getenv( 'BEHAT_RUN' ) ) {
972971
$this->enable_error_reporting();
@@ -1487,6 +1486,10 @@ function( $url, $path, $scheme, $blog_id ) {
14871486
4
14881487
);
14891488

1489+
// Set up hook for plugins and themes to conditionally add WP-CLI commands.
1490+
WP_CLI::add_wp_hook( 'init', function () {
1491+
do_action( 'cli_init' );
1492+
} );
14901493
}
14911494

14921495
/**

0 commit comments

Comments
 (0)