Working with

WP-CLI

Type and run, automate




Jaime Martínez | @jmslbam | #WC024 | 1 sept 2018

Jaime Martínez



Happy coder @ Level Level

Long time WordPress enthousiast

CLI what?

Anyone?


Command
Line
Interface


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

New to the (WP-)CLI?



View my previous WP-CLI talk with some extra references

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

Long running tasks


  • Imports / Exports
  • Regenerating images
  • Bulk processing posts 

Repetitive tasks


  • Cron jobs / nightly imports
  • Base WordPress install
  • Generate test content
  • Flushing rewrite rules

Handy commands


wp core update
wp core update-db 

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

wp user update 1 --user_pass=test

Handy commands

wp rewrite flush

wp option set blogname 

wp media regenerate 

wp transient delete-expired 

Combine commands


No need for new commands

--porcelain  is your friend

Output just the new post id.

Porcelaine


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='Profile picture of Jaime' --porcelain)

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. 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?

A. 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 

B. Simplify multiple steps


wp core multisite-install --url="http://www.hbo.com" --base="http://www.hbo.com" --title="HBO network"  --admin_user=gaya --admin_password=wadup --admin_email="yo@dawg.com" 

# For each line add a new site within the multisite installation

while read show ;
    # Creating multisite {$show}.the-world.com
    wp site create --slug=$show --title=$show --email=yo@dawg.com
done < shows.txt

Deployment


Use it within deployments scripts
See Luc's talk




New stuff



Skip plugins flag

Unload specific broken plugin with fatal error...

wp plugin deactivate my-broken-plugin --skip-plugins=my-broken-plugin
Or just offload all
wp custom-command --skip-plugins



Almost there!

Synopsis

/**
 * @synopsis --handshake[=<secret>] [--a=<b>]
 */ 

Brackets are optional and can be nested
[--handshake[=<secret>]] 

... dots make it possible to pass 1 of multiple arguments

wp post delete 1 3 4 5 66 798 

Any questions?


Jaime Martínez 

@jmslbam / yo@jaimemartinez.nl



WordCamp Nijmegen 2018 - Working with WP-CLI - Barcamp session

By Jaime Martinez

WordCamp Nijmegen 2018 - Working with WP-CLI - Barcamp session

#WC024

  • 1,849