WP-CLI supports using any callable class, function, or closure as a
command. WP_CLI::add_command() is used for both internal and
third-party command registration.
Command arguments are parsed from PHPDoc by default, but also can be
supplied as an optional third argument during registration.
# Register a custom 'foo' command to output a supplied positional param.
#
# $ wp foo bar
# Success: bar
**
* My awesome closure command
*
*
* : An awesome message to display
*
* @when before_wp_load
*\/
$foo = function( $args ) {
WP_CLI::success( $args[0] );
};
WP_CLI::add_command( 'foo', $foo );
public static add_command ( string $name, string $callable, array $args = [] ) : true | ||
$name | string | Name for the command (e.g. "post list" or "site empty"). |
$callable | string | Command implementation as a class, function or closure. |
$args | array | { Optional An associative array with additional registration parameters. 'before_invoke' => callback to execute before invoking the command, 'after_invoke' => callback to execute after invoking the command, 'shortdesc' => short description (80 char or less) for the command, 'synopsis' => the synopsis for the command (string or array), 'when' => execute callback on a named WP-CLI hook (e.g. before_wp_load), } |
Résultat | true | True on success, hard error if registration failed. |