admin管理员组

文章数量:1122832

I'm toying around with writing custom wp-cli commands. I have successfully created a simple command, wp-cli theme save which runs a script to backup the site's theme files:

<?php

class Save_Command extends WP_CLI_Command {

     /**
     * Create a tarball of current active theme and save it to wp_themes directory in the user's config directory.
     *
     * @synopsis
     */
    public function __invoke($args = array(), $assoc_args = array())
    {
    exec ( 'wpst' );
    }

}

WP_CLI::add_command( 'theme save', 'Save_Command' );

This code exists in ~/.wp-cli/commands/custom/save/theme-save.php and works.

Now I want to extend this feature with a new command:

wp theme save colors <scheme_name>

This feature spits out the customized colors to a file. I used the following code to try to get this to work:

<?php

class Save_Theme_Colors_Command extends WP_CLI_Command {

     /**
     * Save color scheme.
     *
     * @synopsis <name of color scheme>
     */
    public function __invoke($args = array(), $assoc_args = array())
    {
    exec ( "go save_theme_colors " . implode(" ", $args));
    }

}

WP_CLI::add_command( 'theme save colors', 'Save_Theme_Colors_Command' );

However, when I try to execute the command I get an error:

Warning: Failed to load autoloader 'phar://wp-cli.phar/vendor/autoload.php'. Reason: 'wp theme save' can't have subcommands.

I've tried various ways to get the subcommand working including using the @subcommand directive but that just seems to get ignored.

I'm toying around with writing custom wp-cli commands. I have successfully created a simple command, wp-cli theme save which runs a script to backup the site's theme files:

<?php

class Save_Command extends WP_CLI_Command {

     /**
     * Create a tarball of current active theme and save it to wp_themes directory in the user's config directory.
     *
     * @synopsis
     */
    public function __invoke($args = array(), $assoc_args = array())
    {
    exec ( 'wpst' );
    }

}

WP_CLI::add_command( 'theme save', 'Save_Command' );

This code exists in ~/.wp-cli/commands/custom/save/theme-save.php and works.

Now I want to extend this feature with a new command:

wp theme save colors <scheme_name>

This feature spits out the customized colors to a file. I used the following code to try to get this to work:

<?php

class Save_Theme_Colors_Command extends WP_CLI_Command {

     /**
     * Save color scheme.
     *
     * @synopsis <name of color scheme>
     */
    public function __invoke($args = array(), $assoc_args = array())
    {
    exec ( "go save_theme_colors " . implode(" ", $args));
    }

}

WP_CLI::add_command( 'theme save colors', 'Save_Theme_Colors_Command' );

However, when I try to execute the command I get an error:

Warning: Failed to load autoloader 'phar://wp-cli.phar/vendor/autoload.php'. Reason: 'wp theme save' can't have subcommands.

I've tried various ways to get the subcommand working including using the @subcommand directive but that just seems to get ignored.

Share Improve this question asked Mar 17, 2019 at 12:55 StevieDStevieD 2112 silver badges10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

OK, looks like I need to change the commands so I have:

wp theme save theme and wp theme save colors

This works.

本文标签: wp cliCreating a subcommand for custom wpcli command