You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add WordPress action to initialize plugin commands on #4818
I think the cli_init hook should be at the earliest possible moment when the "WP-CLI subsystem" has been properly initialized (even if WP might not be fully loaded yet). If people then need to hook into a later stage, they can do something like this:
I think I have a lead on this, based on the comments above and some testing in a sample plugin I created for this ticket.
We should be able to register this action at the top of the start() method in php/WP_CLI/Runner.php, something like:
public function start() {
WP_CLI::add_wp_hook('plugins_loaded', function() {
do_action('cli_init');
});
//...
}
Plugin authors can then do something like:
require_once __DIR__ . '/vendor/autoload.php';
add_action('cli_init', function () {
$command = new \WDS\CLI\CustomCommand();
$command->register_commands();
});
And they will see their command in the listing, as expected.
One thing I tried was to require a class directly (without using a Composer autoloader), and one documentation caveat we would have is that the require statement should be within the hook, as well. I assume, of course, that that's already true today, but it's worth mentioning here, just in case.
One thing I tried was to require a class directly (without using a Composer autoloader), and one documentation caveat we would have is that the require statement should be within the hook, as well. I assume, of course, that that's already true today, but it's worth mentioning here, just in case.
I don't see this as a caveat. We are just providing a hook. This does not override any PHP language requirements attached to this.
Activity
birgire commentedon Jul 19, 2018
I saw the info for the upcoming WP-CLI hack day tomorrow and out of curiosity I had a little source peek :)
I wonder if
php/WP_CLI/Runner.phpwould be a suitable place for this?If so then maybe within the
Runner::setup_bootstrap_hooks()method we could add (with relevant inline docs):First I thought
WP_CLI::do_hook( 'cli_init' );would be needed within the callback, but it seems to only work withdo_action( 'cli_init' );Should the same be done for
admin_init?Is the
inithook the preferred way of hooking (e.g. commands) into WP-CLI?Those who prefer
'plugins_loaded'action instead of'init', should they check if WP_CLI is defined before adding the action, as previosly ?schlessera commentedon Jul 20, 2018
I think the
cli_inithook should be at the earliest possible moment when the "WP-CLI subsystem" has been properly initialized (even if WP might not be fully loaded yet). If people then need to hook into a later stage, they can do something like this:I'm not actually sure yet what the "earliest possible moment" is, though.
jmichaelward commentedon Jul 20, 2018
I think I have a lead on this, based on the comments above and some testing in a sample plugin I created for this ticket.
We should be able to register this action at the top of the
start()method inphp/WP_CLI/Runner.php, something like:Plugin authors can then do something like:
And they will see their command in the listing, as expected.
One thing I tried was to require a class directly (without using a Composer autoloader), and one documentation caveat we would have is that the require statement should be within the hook, as well. I assume, of course, that that's already true today, but it's worth mentioning here, just in case.
schlessera commentedon Jul 20, 2018
I don't see this as a caveat. We are just providing a hook. This does not override any PHP language requirements attached to this.