45
Manage WordPress with Awesome using wp-cli WordCamp Vancouver 2012 Mike Schroder (DH-Shredder) @GetSource - http://www.getsource.net

WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

Embed Size (px)

DESCRIPTION

Manage WordPress with Awesome using wp-cli talk from WordCamp Vancouver 2012. Does updating all your plugins by running `wp plugin update –all` sound too good to be true? Enter wp-cli, an open source WordPress management tool. Learn how to install it locally or globally on your host, perform common WordPress administration tasks, and expand its functionality with plugins of your own. Presented by Mike Schroder (@GetSource/DH-Shredder)

Citation preview

Page 1: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

Manage WordPress with Awesome using wp-cli

WordCamp Vancouver 2012

Mike Schroder (DH-Shredder)@GetSource - http://www.getsource.net

Page 2: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

Who Am I?

• Mike Schroder, a.k.a DH-Shredder, a.k.a. @GetSource

• Third Culture Kid, enjoy Coffee & Sailing

• WordPress Core and wp-cli Contributor

• Happy DreamHost Employee

Page 3: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

There are two groupsof people.

Page 4: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

Those who use the command line

Page 5: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

Those who are going to use the command line

Page 6: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

Don’t be afraid of the CLI.It’s your friend.

Page 7: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

Oh, you like the CLI?wp-cli will make your life better.

Page 8: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

What’s wp-cli?

super-cool Open Source tool to manage WordPress

Page 9: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

Why so cool?

Headed up by Andreas Creten and Cristi Burcă (scribu)

Page 10: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

Why so cool?

Uses WordPress itself to perform operations

Page 11: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

Why so cool?

Automation!

Page 12: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

What can I do with it?

Page 13: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli
Page 14: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

No, Really.

Page 15: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

Update WordPress

wp core update

Page 16: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

Install a Theme

wp theme install sunspot

Page 17: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

Reset to default theme

wp theme activate twentytwelve

Page 18: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

Backup your Database

wp db export backup.sql

Page 19: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

Update Plugins

wp plugin update --all

Page 20: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

What do I need to run it?

Page 21: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

What do I need to run it?

• SSH access to your WordPress install's directory

Page 22: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

What do I need to run it?

• SSH access to your WordPress install's directory

• PHP 5.3+

Page 23: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

What do I need to run it?

• SSH access to your WordPress install's directory

• PHP 5.3+

• WordPress 3.3+

Page 24: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

What do I need to run it?

• SSH access to your WordPress install's directory

• PHP 5.3+

• WordPress 3.3+

• Enough RAM for shell processes to run WordPress

Page 25: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

What do I need to run it?

• SSH access to your WordPress install's directory

• PHP 5.3+

• WordPress 3.3+

• Enough RAM for shell processes to run WordPress

• Easiest on Linux & MacOS

Page 26: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

Okay. Got that covered.How can I get this

Awesomeness?

Page 27: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

Download wp-cli

git clone --recursive git://github.com/wp-cli/wp-cli.git

Page 28: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

Make it runnable from your WordPress Install.

Page 29: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

If you have sudo:

sudo utils/dev-build

Page 30: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

Otherwise, add an alias(.bashrc/.bash_profile)

alias wp='/home/user/wp-cli/src/bin/wp';

Page 31: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

In ~/.bash_profile:

if [ -f ~/.bashrc ]; then source ~/.bashrcfi

(http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html)

Page 32: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

You’ve got it installed?

Let’s dig deeper.

Page 33: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

wp-cli is extensible.

Page 34: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

Add your own directly

wp-cli/src/php/wp-cli/commands/community/cmd_name.php

Page 35: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

Or, define in your plugins.

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

Page 36: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

Sample Plugin:

WCYVR Backup.

Page 37: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

Goal:wp wcyvr backup [--no-db] [/dir/outputfile.tar.gz]

Page 38: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

Our Plan:

- Use built-in SQL Backup- Create a .tar.gz of install and db

Page 39: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

<?php

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

/** * The WCYVR Backup Plugin * * @package WCYVR_Backup * @subpackage commands/community * @maintainer Mike Schroder */class WCYVR_Backup_Command extends WP_CLI_Command {...

Define the Base Command

Page 40: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

class WCYVR_Backup_Command extends WP_CLI_Command {

! function backup( $args, $assoc_args ) {! ! $filename = $dbname = null;! ! ...! }

! public static function help() {! ! WP_CLI::line( "usage: wp wcyvr backup [--no-db] [path/to/file]" );! }}

Define Sub-Commands• $args: stand-alone arguments

• $assoc_args: --arg=value style in associative array

Page 41: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

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';! else! ! $filename = $args[0];

Grab Filename

Page 42: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

! // 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() );! }

Handle --no-db• SQL file not using temp location for simplicity of demo.

Page 43: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

! // 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'." );

Back it up!• See class-wp-cli.php for more magical functions

Page 44: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

! // 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." );! }}

ERROR ERROR

Page 45: WordCamp Vancouver 2012 - Manage WordPress with Awesome using wp-cli

Resources!• https://github.com/wp-cli/wp-cli

• https://github.com/wp-cli/wp-cli/wiki/List-of-internal-commands

• https://github.com/wp-cli/wp-cli/wiki/Commands-Cookbook

• http://scribu.net/wordpress/a-command-line-interface-for-wordpress.html

• http://wp.tutsplus.com/tutorials/using-wp-cli-for-fun-and-profit/

• http://halfelf.org/2012/command-line-wp/

• http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html

Mike Schroder (DH-Shredder)@GetSource - http://www.getsource.net