Skip to content

Add WordPress action to initialize plugin commands on #4818

Closed

Description

@schlessera

Right now, when a plugin wants to add WP-CLI actions, they need to do something like the following:

if ( defined( 'WP_CLI' ) && WP_CLI ) {
	add_action( 'plugins_loaded', 'my_plugin_cli_init' );
}

We should have WP-CLI add an action 'cli_init' that matches 'init' and 'admin_init' and allows for easier hooking of commands:

add_action( 'cli_init', 'my_plugin_cli_init' );

If WP-CLI is not loaded, the action will not be triggered. No other checks are needed.

Activity

  1. birgire commented on Jul 19, 2018

    @birgire

    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.php would be a suitable place for this?

    If so then maybe within the Runner::setup_bootstrap_hooks() method we could add (with relevant inline docs):

    WP_CLI::add_wp_hook( 'init', function(){
    	do_action( 'cli_init' );
    } );
    
    

    First I thought WP_CLI::do_hook( 'cli_init' ); would be needed within the callback, but it seems to only work with do_action( 'cli_init' );

    Should the same be done for admin_init ?

    Is the init hook 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 ?

  2. schlessera commented on Jul 20, 2018

    @schlessera
    MemberAuthor

    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:

    add_action( 'cli_init' function () {
       add_action( 'plugins_loaded', [ $this, 'prepare_all_commands' ] );
    }
    

    I'm not actually sure yet what the "earliest possible moment" is, though.

  3. jmichaelward commented on Jul 20, 2018

    @jmichaelward

    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.

  4. schlessera commented on Jul 20, 2018

    @schlessera
    MemberAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions