Hitchhiker's guide

to

Arūnas Liuiza

WordPress Core Contributor, WordPress Kaunas Meetup co-organizer, WordCamp (Lithuania, Riga, Stockholm, Jyväskylä, Oslo, Norrköping) speaker and one of the editors of the Lithuanian WordPress translation team.

 

Free & premium WordPress plugin developer

 

Software Engineer at

For Developers

deployer.seravo.com - a service that syncs WordPress plugins from GitHub to WordPress.org automatically.
 

TryoutWP.com - a service to spin up live temporary demo sites for WordPress plugins and themes.

The Answer to Life, the Universe
and Everything?

Any sufficiently advanced technology is indistinguishable from magic

- Arthur C. Clarke,1973

/whois WP-CLI

The official command line tool for interacting with and managing your WordPress sites.

 

Yup, the Terminal.

 

Official website: https://wp-cli.org/

Requirements

  • UNIX-like environment (OS X, Linux, FreeBSD, Cygwin);
    • limited support in Windows environment
       
  • PHP 5.3.29 or later.
     
  • WordPress 3.7 or later.
    • Versions older than the latest WordPress release may have degraded functionality

Installation

  • Download the wp-cli.phar file using wget or curl:
    • curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
      
      
  • Then, check if it works:
    • php wp-cli.phar --info
      

Installation (2)

  • To be able to type just wp, instead of php wp-cli.phar, you need to make the file executable and move it to somewhere in your PATH. For example:
    • chmod +x wp-cli.phar
      sudo mv wp-cli.phar /usr/local/bin/wp
      
      
  • Now try running:
    • wp --info
      

Updates

  • Just run this:
    • wp cli update
      
      

The Basics

Anatomy of a Command

wp plugin update akismet --version=1.0.0
wp command action input --parameter=value

 

Global Parameters

--path=            a path to s WordPress directory

--url=               url of the site

--user=            set user

--skip-themes[=theme]

--skip-plugins[=plugin]

--dry-run         

--format=json|table|csv

...

https://make.wordpress.org/cli/handbook/config/#global-parameters

wp plugin <action>

wp plugin install tinycoffee

 

wp plugin activate tinycoffee

 

wp plugin update tinycoffee --version=0.1.0

 

https://developer.wordpress.org/cli/commands/plugin/update/

wp theme <action>

wp theme install twentyseventeen

 

wp theme install twentyseventeen

 

wp theme update twentyseventeen --version=0.1.0

 

https://developer.wordpress.org/cli/commands/theme/update/

wp user <action>

wp user list

 

wp user create bob bob@example.com --role=author

 

wp user update 123 --display_name=Mary --user_pass=marypass


https://developer.wordpress.org/cli/commands/user/​

wp ... anything

wp config

wp core

wp cache

wp comment

wp cron

wp db

wp export

wp import

wp media

wp menu

wp network

wp option

wp package

wp post

wp post-type

wp profile

wp rewrite

wp role

wp server

wp sidebar

wp search-replace

wp site

wp super-admin

wp taxonomy

wp term

wp transient

wp widget

...

Common tasks

Update WordPress

wp core update

 

wp core update-db

 

wp core update-db --network

Install WordPress

wp core download --locale=lt_LT

wp config create --dbname=testing --dbuser=wp
--dbpass=securepswd

 

wp core install --url=example.com --title=Example
--admin_user=supervisor --admin_password=strongpassword --admin_email=info@example.com

Import/export

wp export

 

wp import example.wordpress.2018-08-10.xml

Database

wp db export backup.sql

 

wp db import backup.sql

Search & Replace

wp search-replace olddomain.com newdomain.com 'wp_post*'

 

  • works with serialised data
     
  • can do regular expressions
     
  • might be a little... slow :)

 

wp db search - another option

Scaffolds

Generates code for various common developer tasks:

  • plugins
  • themes
  • child-themes
  • custom post types
  • custom taxonomies
  • Gutenberg blocks

 

wp scaffold plugin new-plugin slug

Packages

WP-CLI packages are community-maintained projects built on WP-CLI. They can contain WP-CLI commands, but they can also just extend WP-CLI in some way.

 

http://wp-cli.org/package-index/

 

  • Available on all WordPress installs, as opposed to just where the plugin is activated.
  • Can run early, before WordPress is loaded.

 

wp package install

The Magic

Plugin status accros MS

wp site list --field=domain |
xargs -I %a% bash -c
"printf \"%a% \"
&&
wp plugin list --name=akismet --url=%a% --field=status

 

TIL - there is a package for that...

Get/Set Nested Options

wp site list --field=domain |
xargs -I %a%
bash -c
"printf \"%a%\"
&&
wp option pluck comet_cache_options enable --url=%a%"

 

 

replace "pluck" with "patch" to set the option

Find ID's of inline images

db search "wp-image-([0-9]+)" wp_posts --regex
--matches_only --before_context=0 --after_context=0
| grep -oP "(?<=wp-image-)[0-9]*"

 

DIY

Build Your Own Commands

if ( class_exists( 'WP_CLI' ) ) {
  WP_CLI::add_command( 'hello', 'arunas_cli_hello' );
}
function arunas_cli_hello( $args, $options ) {
  WP_CLI::success( 'Hello, World!' );
}
    

Questions?

Hitchhiker's guide to WP-CLI

By Arūnas Liuiza

Hitchhiker's guide to WP-CLI

WordCamp Norrköping 2018

  • 1,512