Slide 1

Slide 1 text

Mike  Schroder  |  @GetSource   Developer,  WordPress  Specialist  at  DreamHost   MAGICAL WORDPRESS MANAGEMENT USING WP-CLI WordCamp  San  Francisco  2013  

Slide 2

Slide 2 text

RANDOM FACTS   Third  Culture  Kid   Enjoys  Coffee  &  Sailing   Blogs  at  getsource.net   CREDENTIALS   Mike  Schroder,  a.k.a.  DH-­‐Shredder,  a.k.a  @GetSource   Happy  DreamHost  Employee   WordPress  Core  Contributor   WordPress  3.5  Recent  Rockstar   Trusted  WP-­‐CLI  Feature  Contributor   Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 3

Slide 3 text

THERE ARE TWO GROUPS OF PEOPLE. Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 4

Slide 4 text

THOSE WHO USE THE COMMAND LINE Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 5

Slide 5 text

THOSE WHO ARE GOING TO USE THE COMMAND LINE Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 6

Slide 6 text

DON’T BE AFRAID OF THE CLI IT’S  YOUR  FRIEND.   Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 7

Slide 7 text

OH, YOU LIKE THE CLI? WP-­‐CLI  WILL  MAKE  YOUR  LIFE  BETTER   Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 8

Slide 8 text

SUPER COOL OPEN SOURCE TOOL TO MANAGE WORDPRESS Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 9

Slide 9 text

HEADED UP BY CRISTI BURCĂ (SCRIBU) AND ANDREAS CRETEN Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 10

Slide 10 text

USES WORDPRESS ITSELF TO PERFORM OPERATIONS Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 11

Slide 11 text

AUTOMATION! Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 12

Slide 12 text

WHAT CAN I DO WITH IT? Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 13

Slide 13 text

Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 14

Slide 14 text

NO, REALLY. Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 15

Slide 15 text

$ wp core update UPDATE WORDPRESS Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 16

Slide 16 text

$ wp plugin install INSTALL A PLUGIN Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 17

Slide 17 text

$ wp theme activate CHANGE THEME Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 18

Slide 18 text

$ wp db export [filename] BACKUP YOUR DATABASE Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 19

Slide 19 text

$ wp plugin update-all UPDATE PLUGINS Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 20

Slide 20 text

$ wp theme update-all UPDATE THEMES Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 21

Slide 21 text

$ wp search-replace REPLACE TEXT IN DB Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 22

Slide 22 text

WHAT IS THIS SORCERY? Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 23

Slide 23 text

Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 24

Slide 24 text

SOLD. HOW DO I GET THIS AWESOMENESS? Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 25

Slide 25 text

-­‐    SSH  access  to  your  WordPress  install's  directory   -­‐    PHP  5.3.2+   -­‐    WordPress  3.4+   -­‐    Enough  RAM  for  shell  processes  to  run  WordPress   -­‐    Linux  or  MacOS  (for  now)   WHAT DO I NEED TO RUN IT? Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 26

Slide 26 text

$ curl https://raw.github.com/wp-cli/wp- cli.github.com/master/installer.sh | bash INSTALL IN LOCAL USER Full Instructions at: wp-­‐cli.org     Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 27

Slide 27 text

BUT WAIT, THERE’S MORE! Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 28

Slide 28 text

WP-CLI IS EXTENSIBLE! Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 29

Slide 29 text

Run/Alias:     $ wp --require='cmd_name.php’ wp-­‐cli.yml   require: /path/to/cmd_name.php ADD YOUR OWN LOCALLY Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 30

Slide 30 text

OR IN YOUR PLUGINS if ( defined('WP_CLI') && WP_CLI ) { include( PLUGIN_DIR . '/lib/wp-cli.php' ); } Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 31

Slide 31 text

$ wp migrate backup [file.tar.gz] [--no-db] [--db-name=] CUSTOM COMMAND: BACKUP! Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 32

Slide 32 text

Strategy -­‐    Use  Built-­‐in  SQL  Backup  Command   -­‐    Create  .tar.gz  of  install’s  ciles  and  database.   CUSTOM COMMAND: BACKUP! Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 33

Slide 33 text

ADD COMMAND

Slide 34

Slide 34 text

DEFINE YOUR SUBCOMMAND /** * Backup entire WordPress install, including core, plugins and database. * * @param array $args * @param array $assoc_args * @synopsis [backup_filename] [--no-db] [--db-name=] */ function backup( $args, $assoc_args ) { // ... $args Stand-­‐alone  arguments   $assoc_args --arg=value  style  in  associative  array   Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 35

Slide 35 text

DEFINE YOUR SUBCOMMAND /** * Backup entire WordPress install, including core, plugins and database. * * @param array $args * @param array $assoc_args * @synopsis [backup_filename] [--no-db] [--db-name=] */ function backup( $args, $assoc_args ) { // ... @synopsis WP-­‐CLI  specicic  comment  to  automatically  force  proper  CLI  syntax   Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 36

Slide 36 text

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'; else $filename = $args[0]; // ... Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 37

Slide 37 text

// 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() ); } // ... Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 38

Slide 38 text

// 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_CLI\Utils\esc_cmd( " tar \ --exclude '.git' \ --exclude '.svn' \ --exclude '.hg’ \ --exclude '.bzr' \ -czf %s . -C %s %s \ ", $filename, $backup_directory, $dbname ), false ); // ... Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 39

Slide 39 text

// 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." ); } } // End backup } // End DH_Migrate_Command Mike  Schroder  |  @GetSource  |  #wcsf  2013    

Slide 40

Slide 40 text

    -­‐    wp-­‐cli.org   -­‐    getsource.net/tag/wp-­‐cli/   -­‐    halfelf.org/2012/command-­‐line-­‐wp       -­‐    ciles.fosswire.com/2007/08/fwunixref.pdf     FURTHER LEARNIFICATION: WP-CLI: CLI Cheat Sheet: Mike  Schroder  |  @GetSource  |  #wcsf  2013