Download - Manage WordPress with Awesome using wp cli

Transcript
Page 1: Manage WordPress with Awesome using wp cli

Manage WordPress with Awesome using wp-cli

WordCamp Vegas 2012

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

Page 2: 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: Manage WordPress with Awesome using wp cli

There are two groupsof people.

Page 4: Manage WordPress with Awesome using wp cli

Those who use the command line

Page 5: Manage WordPress with Awesome using wp cli

Those who are going to use the command line

Page 6: Manage WordPress with Awesome using wp cli

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

Page 7: Manage WordPress with Awesome using wp cli

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

Page 8: Manage WordPress with Awesome using wp cli

What’s wp-cli?

super-cool Open Source tool to manage WordPress

Page 9: Manage WordPress with Awesome using wp cli

Why so cool?

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

Page 10: Manage WordPress with Awesome using wp cli

Why so cool?

Uses WordPress itself to perform operations

Page 11: Manage WordPress with Awesome using wp cli

Why so cool?

Automation!

Page 12: Manage WordPress with Awesome using wp cli

What can I do with it?

Page 13: Manage WordPress with Awesome using wp cli
Page 14: Manage WordPress with Awesome using wp cli

No, Really.

Page 15: Manage WordPress with Awesome using wp cli

Update WordPress

wp core update

Page 16: Manage WordPress with Awesome using wp cli

Install a Theme

wp theme install sunspot

Page 17: Manage WordPress with Awesome using wp cli

Reset to default theme

wp theme activate twentytwelve

Page 18: Manage WordPress with Awesome using wp cli

Backup your Database

wp db export backup.sql

Page 19: Manage WordPress with Awesome using wp cli

Update Plugins

wp plugin update --all

Page 20: Manage WordPress with Awesome using wp cli

What do I need to run it?

Page 21: Manage WordPress with Awesome using wp cli

What do I need to run it?

• SSH access to your WordPress install's directory

Page 22: 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: 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: 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: 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: Manage WordPress with Awesome using wp cli

Okay. Got that covered.How can I get this

Awesomeness?

Page 27: Manage WordPress with Awesome using wp cli

Download wp-cli

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

Page 28: Manage WordPress with Awesome using wp cli

Make it runnable from your WordPress Install.

Page 29: Manage WordPress with Awesome using wp cli

If you have sudo:

sudo utils/dev-build

Page 30: 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: 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: Manage WordPress with Awesome using wp cli

You’ve got it installed?

Let’s dig deeper.

Page 33: Manage WordPress with Awesome using wp cli

wp-cli is extensible.

Page 34: Manage WordPress with Awesome using wp cli

Sample Plugin:

WCLV Backup.

Page 35: Manage WordPress with Awesome using wp cli

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

Page 36: Manage WordPress with Awesome using wp cli

Our Plan:

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

Page 37: 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( '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 {...

Define the Base Command

Page 38: Manage WordPress with Awesome using wp cli

class WCLV_Backup_Command extends WP_CLI_Command {

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

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

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

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

Page 39: 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 40: 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 41: 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 42: 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 43: 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