WP-CLI is a PHP script that interacts with WordPress from the command line (Unix).
WP-CLI is not, itself, a plugin.WP-CLI works on your server (or local dev environment). It is not necessarily part of WordPress itself.
WP-CLI lets you work with nearly all parts of WordPress, content, users, settings, plugins, etc.
WP-CLI has a simple API which makes it easily and infinitely extensible.
By default WP-CLI only works with WordPress' core functionality. Extending WP-CLI lets you work with:
WP_CLI::add_command( 'hello', 'Hello_World_Command' );
class Hello_World_Command extends WP_CLI_Command {
public function __invoke() {
WP_CLI::success( 'Hello World' );
}
}
$ wp hello
class Hello_World_Command extends WP_CLI_Command {
/**
* An improved Hello World
*
* @synopsis [<NAME>] [--name=<NAME>]
*/
public function __invoke( $args, $assoc_args) {
$name = 'World';
if ( isset( $args[0] ) || isset( $assoc_args['name'] ) ) {
$name = isset( $args[0] ) ? $args[0] : $assoc_args['name'];
}
WP_CLI::success( 'Hello ' . sanitize_text_field( $name );
}
}
$ wp hello Chris
or
$ wp hello --name=Chris
class Hello_World_Command extends WP_CLI_Command {
public function world() {
WP_CLI::success( 'Hello World' );
}
/**
* An improved Hello World
*
* @synopsis <NAME>
*/
public function name( $args, $assoc_args) {
WP_CLI::success( 'Hello ' . sanitize_text_field( $args[0] );
}
}
$ wp hello world
or
$ wp hello name Chris
if ( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::add_command( 'hello', 'Hello_World_Command' );
}
WP_CLI::success( 'You Did Good' );
WP_CLI::error( "You Failed. I'm done" );
WP_CLI::error will, if called like success, terminate the script. Success will just go on
You can set the 2nd param, exit, to false on WP_CLI::error to continue execution
WP_CLI::error( "You Failed, but I'll keep going anyway.", false );
You can automattically output data in various formats:
WP_CLI\Utils\format_items( 'json', get_users(), array( 'ID', 'user_login' ) );
WP_CLI\Utils\format_items( 'table', get_users(), array( 'ID', 'user_login' )
);
$notify = \WP_CLI\Utils\make_progress_bar( 'Generating posts', $current_post_count
);
$notify->tick();
$notify->finish();
Various [not well documented] utility function in php/utils.php
The path variable lets you execute a command from anywhere
Target file only needs the command registration and location of command class (require statement)
One command repo on the server/environment/etc can execute commands for all sites