This document discusses extending the functionality of WP-CLI, the command line interface for WordPress, by creating custom commands. It begins with an overview of the basic commands included out of the box. It then explains that more can be done by installing additional packages and provides a link to the package index. The document outlines how to create custom commands as functions or classes and pass arguments to them. It lists WP-CLI's internal API for output, input, execution and other tasks. It provides links to documentation and explains how the author uses WP-CLI for dynamic scaffolding in their starter theme.
9. wp about
wp about
--------------------------------------------------
- Site Information
- Site Title : Sinebridge
- Tagline : Cloud Architecture, Design & Development
- Wp Address (URL) : http://sinebridge.com
- Site Address (URL) : http://sinebridge.com
- E-mail Address : support@sinebridge.com
- Current theme name : sinebridge
--------------------------------------------------
- Database Information
- Hostname : 127.0.0.1
- Database Name : database-name
- Username : database-username
- Password : ****************
--------------------------------------------------
- Miscellaneous Environment Information
- Wordpress version : 3.8.1
- Database revision : 26691
- Multisite install : false
- PHP version : 5.5.x-xx
- OS Hostname : localhost
- OS Type : Linux
- OS Version : #49-Ubuntu SMP Tue Nov 12 18:00:10 UTC 2013
- OS Release : 3.8.0-34-generic
- Machine type : x86_64
9
12. Custom Commands
1. Can be defined in plugins or themes
2. Can be easily scaffolded as standalone projects
with wp scaffold package
3. Can be distributed independent of a plugin or
theme in the aforementioned Package Index
12
13. Command as Function
function foo_command( $args, $assoc_args ) {
WP_CLI::success( $args[0] );
}
WP_CLI::add_command( 'foo', 'foo_command' );
13
14. Command as Object
class Foo_Command {
public function __invoke( $args, $assoc_args ) {
WP_CLI::success( $args[0] );
}
}
WP_CLI::add_command( 'foo bar', 'Foo_Command' );
14
15. $args
15
$ wp example hello Joe Doe
WP_CLI::line( $args[0] ); // Joe
WP_CLI::line( $args[1] ); // Doe
16. $assoc_args
16
$ wp example hello --name='Joe D.'
WP_CLI::line( $assoc_args['name'] ); // Joe D.
$ wp example hello --verbose --no-option
WP_CLI::line( $assoc_args['verbose'] ); // true
WP_CLI::line( $assoc_args['option'] ); // false
17. Combine them…
17
$ wp example hello --name=Joe foo --verbose bar
WP_CLI::line( $assoc_args['name'] ); // Joe
WP_CLI::line( $assoc_args['verbose'] ); // true
WP_CLI::line( $args[0] ); // foo
WP_CLI::line( $args[1] ); // bar
20. Output
• WP_CLIUtilsformat_items() – Render a collection of items as an ASCII
table, JSON, CSV, YAML, list of ids, or count.
• WP_CLIUtilsmake_progress_bar() – Create a progress bar to display
percent completion of a given operation.
• WP_CLI::colorize() – Colorize a string for output.
• WP_CLI::success() – Display success message prefixed with "Success: ".
• WP_CLI::debug() – Display debug message prefixed with "Debug: " when `–
debug` is used.
• WP_CLI::warning() – Display warning message prefixed with "Warning: ".
• WP_CLI::error() – Display error message prefixed with "Error: " and exit script.
• WP_CLI::error_multi_line() – Display a multi-line error message in a red box.
• WP_CLIUtilswrite_csv() – Write data as CSV to a given file.
20
21. Input
• WP_CLIUtilslaunch_editor_for_input() – Launch system’s $EDITOR for the user
to edit some text.
• WP_CLIUtilsget_flag_value() – Return the flag value or, if it’s not set, the $default
value.
• WP_CLIUtilsreport_batch_operation_results() – Report the results of the same
operation against multiple resources.
• WP_CLIUtilsparse_str_to_argv() – Parse a string of command line arguments into
an $argv-esqe variable.
• WP_CLI::confirm() – Ask for confirmation before running a destructive operation.
• WP_CLI::read_value() – Read a value, from various formats.
• WP_CLI::get_config() – Get values of global configuration parameters.
21
22. Execution
• WP_CLI::launch() – Launch an arbitrary external process
that takes over I/O.
• WP_CLI::launch_self() – Run a WP-CLI command in a new
process reusing the current runtime arguments.
• WP_CLI::runcommand() – Run a WP-CLI command.
• WP_CLI::run_command() – Run a given command within
the current process using the same global parameters.
22
23. Other
• WP_CLIUtilshttp_request() – Make a HTTP request to a remote URL.
• WP_CLIUtilsget_named_sem_ver() – Compare two version strings
to get the named semantic version.
• WP_CLIUtilsparse_ssh_url() – Parse a SSH url for its host, port, and
path.
• WP_CLIUtilsisPiped() – Checks whether the output of the current
script is a TTY or a pipe / redirect
• WP_CLIUtilsesc_like() – First half of escaping for LIKE special
characters % and _ before preparing for MySQL.
23