WCSF 2013 – Magical WordPress Management using WP-CLI

I was humbled to be invited to speak at WordCamp San Francisco this year at the last minute, and I’m excited to share it with you all!

You can find the slides in PDF here.

Embedded slides and sample plugin code available after the break.

[edit] Slides Updated! Full Details below. [/edit]

Included below is a simple backup plugin — you’ll probably want to change default SQL dump storage locations for your uses in production, but it’s a good start!

If you have any questions, let me know in the comments!


WP_CLI::add_command( 'migrate', 'DH_Migrate_Command' );

 * DreamHost Migrate Plugin
 * @package DH_Migrate_Command
 * @subpackage commands/community
 * @maintainer Mike Schroder
class DH_Migrate_Command extends WP_CLI_Command {

	 * Backup entire WordPress install, including core, plugins and database.
	 * @param array $args
	 * @param array $assoc_args
	 * @synopsis [backup_filename] [--no-db] [--db-name=<filename>]
	function backup( $args, $assoc_args ) {
		$filename = $dbname = null;
		$backup_directory = '../';

		// If a filename isn't specified, default to "Site's Title.tar.gz".
		if ( empty( $args ) )
			$filename = $backup_directory . 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 = isset( $assoc_args['db-name'] ) ? $assoc_args['db-name'] : 'database_backup.sql';

			WP_CLI::run_command( array( 'db', 'export', $backup_directory . $dbname ), array() );

		// Using esc_cmd to automatically escape parameters.
		// We can't use --exclude-vcs, because it's not available on OSX.
		WP_CLI::line( "Backing up to $filename ..." );
		$result = WP_CLI::launch( WP_CLIUtilsesc_cmd( "
				--exclude '.git'	
				--exclude '.svn'	
				--exclude '.hg'		
				--exclude '.bzr'	
				-czf %s . -C %s %s	
		", $filename, $backup_directory, $dbname ), false );

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

		if ( 0 == $result ) {
			WP_CLI::success( "Backup Complete." );
		} else {
			WP_CLI::error( "Backup Failed." );

11 thoughts on “WCSF 2013 – Magical WordPress Management using WP-CLI”

  1. Re: invoking WP-CLI on Vagrant from host machine, I made a convenience wrapper called vassh: https://gist.github.com/westonruter/5992510

    The big pain of doing `vagrant ssh` is that it doesn’t drop you into the corresponding working directory in the Vagrant guest’s synced_folder, so you have to `cd` to the dir and then run `wp`. So `vassh` will make sure you start out in the corresponding directory. So if you’re in your WordPress project on your host machine, all you need to do is:

    $ vassh wp core update

    There’s also a wrapper called `vasshin` which will shell you into Vagrant at the current directory, with a prompt for entering commands. This gets you colors and interactive tty. You can also pass commands as arguments to `vasshin` to have them executed right away in the colorized tty (with some additional Vagrant .bash_login echoes and SSH connection close):

    $ vasshin wp post list # nice table!

  2. Hey Mike,

    Thanks a lot for giving the talk!

    One small thing: I wish you hadn’t suggested people drop custom commands inside the wp-cli/ folder. They will likely get deleted once you update WP-CLI.

    1. Thanks for the note!

      Hadn’t thought of that, since many of my installs are GIT checkout based (due to legacy reasons). I’ll update the post (and then the slides) to reflect that.

      [edit] Slides Updated, with full details at end of post. [/edit]

  3. Hi I have a quick question. I want to know if WP-CLI is updated on the current version of wordpress. I want to do an update to my wordpress but I don’t want to run into any problems.

    Thank you

    1. You’ll still want to be sure the plugins and themes you’re using are compatible with the latest version of WordPress, but WP-CLI uses WordPress’ update functions, so it doesn’t need to be updated each time WordPress has a new release to give you the newest version.

Leave a Reply

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