Manage WordPress with Awesome using wp-cli

I spoke at WordCamp Vegas 2012 on wp-cli.

It was great to see so many people excited to use the command line!

Thanks to John Hawkins and the organizing to team for a great WordCamp, and the chance to speak.

I’ll be updating this post with additional references and notes, so please let me know if you have any questions in the comments!

Slides and sample code below.

Download slides via PDF

Sample plugin code:

Note that this is a simplified version of the command, so you will likely want to change how the temporary file saving works for general purpose use.


// Let WP_CLI know we exist!
// Earlier versions of wp-cli used WP_CLI::addCommand()
WP_CLI::add_command( 'wclv', 'WCLV_Backup_Command' );

 * The WCLV Backup Plugin
 * @package WCLV_Backup
 * @subpackage commands/community
 * @maintainer Mike Schroder
class WCLV_Backup_Command extends WP_CLI_Command {

	 * Backup your WordPress install.
	 * @param array $args
	 * @param array $assoc_args
	function backup( $args, $assoc_args ) {
		$filename = $dbname = null;

		// If a filename isn't specified, default to "Site's Title.tar.gz".
		if ( empty( $args ) )
			$filename = '../' . escapeshellarg( get_bloginfo() ) . '.tar.gz';
			$filename = $args[0];

		// If --no-db is specified, don't include the database in backup
		if ( ! isset( $assoc_args['no-db'] ) ) {
			$dbname = '../database_temp.sql';

			// This is cheating a bit, since wp-cli doesn't currently support
			// running commands within commands without re-launching itself.
			WP_CLI::run_command( array( 'db', 'export', $dbname ), array() );

		// GZ/Tar and Backup the install!
		WP_CLI::line( "Backing up to '$filename' ..." );
		$result = WP_CLI::launch( "tar -zcvf $filename . $dbname", false );

		// If we created a database backup, remove the temp file.
		if ( $dbname && ! unlink( $dbname ) )
			WP_CLI::warning( "Couldn't remove temporary database backup, '$dbname'." );

		// Will automatically exit on WP_CLI::error, but not WP_CLI::success.
		if ( 0 == $result ) {
			WP_CLI::success( "Backup Complete." );
		} else {
			WP_CLI::error( "Backup Failed." );

	 * Output syntax for command
	public static function help() {
		WP_CLI::line( "usage: wp wclv backup [--no-db] [path/to/file]" );


I had a few questions on specific code for adding a command to a plugin.
This is fortunately super easy.

Create your wp-cli command just as you would per the slides, then use the following code to only add your command when the plugin is getting loaded via wp-cli (initial code courtesy of Mika’s DreamObjects plugin).

if ( defined('WP_CLI') && WP_CLI ) {
	include( PLUGIN_DIR . '/lib/wp-cli.php' );

10 thoughts on “Manage WordPress with Awesome using wp-cli”

  1. Pingback: | WordCamp
  2. Hi MIke. I just saw your great video talk and you sell the command line & WP CLI well, which made me want to try it out and use it. You encouraged new command line user to start using it more. I have used the command very little. I have recently published my first plugin and this is where I got into the command line for the first time. But for new users to the command line, it can very quickly get hairy. For instance, I followed the WP CLI install instructions and it all went well it seemed, but when I tested wp –info, I got “command not found”. Searching google for a solution got me no where. It also said something about adding something to .bashrc. Furthermore, it says something about using bash or Zsh and I have no idea what this is. And this is not the first time I have found no solution to a command line issue. It’s easy to get stuck.
    So my question is, how much command line should I know before getting into WP CLI and is there some degree of command line level that one should contain before getting into this kind of stuff? Where should one start and where should one get support? I ask you, because you make it sound so easy. 🙂
    I hope you can help. 🙂

    1. Google is usually a pretty good resource for details on the command line!
      I think if you know how to open a command prompt, then you may as well start playing with various commands, including WP-CLI.

      What platform are you trying to install WP-CLI on? Most use bash by default.

      On Linux/MacOS, check out this article for the technical details on which file you should add to.

      You have two choices on what you add to your .bashrc or .bash_profile — you either add an alias, which is a sort of a “shortcut” that links to a command, or you can add the WP-CLI bin directory to your path. Either will make the wp command available anywhere.

      The suggested way at the end of the standard WP-CLI install script is to add to your .bashrc:
      export PATH=$INSTALL_DIR/bin:$PATH
      (where $INSTALL_DIR is the path to your WP-CLI installation)

      Hopefully this helps!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.