Simplify your day to day tasks with WP-CLI

Type and run, automate




Jaime Martínez | @jmslbam | #wpmeetup010 | 25 nov 2013

 

Hola

Jaime Martinez



Happy coder @ Level Level

Feature contributor and tester WP-CLI

Long time WordPress enthousiast


Let me seduce you to the 
WP-CLI side

Command what?


Command
Line
Interface


A direct, text-based, interface to a computer.

New to the CLI?



Anyone some more source?

Made by


Setup by Andreas Creten.
Actively maintained by Cristi Burcă a.k.a. Scribu  & Daniel Bachhuber

And many more ...

Why WP-CLI?


Uses WordPress to perform operations

  • Run long running tasks like without time-outs
  • Repetitive tasks
  • Saves time
  • Extensible with your own commands

Install WP-CLI


curl https://raw.github.com/wp-cli/wp-cli.github.com/master/installer.sh | bash 


Full instructions: wp-cli.org

Default location: ~/.wp-cli/

Add  ~/.wp-cli/bin/ to your $PATH


MAMP? Thank you Luc

Use Vagrant


Salty WordPress from HumanMade

Using it live?


SSH access to your server


Exeception for WP Remote through the wp-remote-cli package.


Again


Why WP-CLI?

Long running tasks


  • Imports / Exports
  • Regenerating images
  • Bulk processing posts 

Repetitive tasks


  • Cron jobs
  • Base WordPress install

Handy commands


wp core update
wp core update-db 

wp db import
wp search-replace http://nu.nl http://nu.dev

wp user update 1 --user_pass=test

wp media regenerate 

wp scaffold


Don’t copy & paste

wp scaffold cpt|tax|theme|plugin  

More info this topic on wp-cli.org

Combine commands


No need for new commands

--porcelain  is your friend

A. Bulk publish posts


wp post update $(wp post list --post_type='zombie' --format=ids) --post_status=publish
step 1
wp post list --post_type='zombie' --format=ids
step 2
wp post update 33 45 68 --post_status=publish

B. Regenerate last 50 uploaded images


wp media regenerate  $(wp post list --post_type=attachment --format=ids --posts_per_page=50)

C. Create Post & Attach Image


Use the return value (post_id) to attach it to the imported file.
wp media import ~/Pictures/jaimemartinez.png --post_id=$(wp post create --post_name=wpmeetup --post_title=test1 --porcelain)

D. Update multi-site installation

#!/bin/bash
#
for url in $(wp site list --fields=url --format=csv | tail -n +2)do wp --url=$url core update-dbdone



Nice, what else?

Simplify multiple steps


It’s no composer but it does the job

cat plugins.txt | xargs wp plugin install

advanced-custom-fields
https://github.com/Jeradin/acf-website-field/archive/master.zip
wordpress-seo 

Simplify multiple steps


Bash install script for a clean WordPress installation

wp db migrate


Not a real command! But you can mimic it.

wp db migrate


wp db import dump.sql
wp search-replace http://wp.dev http://wp.nl
wp db export prod.wp.sql

Borrow code!


User import cron

Starting point was  wp user import-csv



Almost there!

Extending WP-CLI


Community packages
Community commands

Community commands


WordPress Plugins can add commands

Available per WordPress install

<?php // Plugin Name: Sweet Plugin if ( defined('WP_CLI') && WP_CLI ) { include __DIR__ . '/my-command.php'; }


Community packages


These commands are globally available for every install

Which packages?
Add them to the ~/.wp-cli/composer.json

How to install community packages

Any questions?


Jaime Martinez


j@jaimemartinez.nl

WP-CLI - WP meetup010 - 25 nov 2013

By Jaime Martinez

WP-CLI - WP meetup010 - 25 nov 2013

  • 11,201