Chrome for Android fix.

Controlling WordPress through the Command Line

Introduction to WP-CLI

Alain Schlesser
Software Engineer & WordPress Consultant

The Command Line

What is the command line?

  • Text interface to interact with computer

  • Less visual feedback than a GUI *

  • More expressive than a GUI

* Graphical User Interface

Why type when you can point and click?

GUI - 1 Site

CLI - 1 Site

point - click,
point - click,
point - click


                        $ wp user set-role alain admin
                        Success: Added user (1) to http://wp.test as admin.
                        $ _
                    

1 minute of work

1 minute of work

GUI - 10000 Sites

CLI - 10000 Sites

point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click, point - click,

...


                        $ wp site list --field=url | xargs -n1 -I %
                          wp --url=% user set-role alain admin
                        Success: Added user (1) to http://wp.test as admin.
                        Success: Added user (1) to http://1.wp.test as admin.
                        Success: Added user (1) to http://2.wp.test as admin.
                        Success: Added user (1) to http://3.wp.test as admin.
                        ...
                    

6.9 days of work

5 minutes of work

Prompt

The shell prompt is the entry point where you can type your commands.


                $ _
                 
                 
                 
                 
                 
                 
                 
            

Launching an application

Just enter the name of the application:


                $ <name-of-program>
                <output-of-program>
                $ _
                 
                 
                 
                 
                 
            

⇒ magic stuff will happen!

DEMO
launching an application from the command line

  • lslist directory contents

Arguments (1/2)

Syntax

ls [OPTION]... [FILE]...

Options (subset)

  • -a, --allshow hidden entries
  • -luse a long listing format

Arguments (2/2)

Short & Long Options

--all   ==   -a

Combined Short Options

-a -l   ==   -al

DEMO
using arguments to modify application behavior

  • echo [STRING]echo a string to standard output

Autocompletion

  • (Tab key)autocomplete current command/argument
  • If current text only results in 1 match, that match is used.

  • If current text results in more matches, available options are shown. Reduce set by typing additional characters and try again.

DEMO
using autocompletion

Unix Philosophy

  • Write programs that do one thing and do it well.

  • Write programs to work together.

  • Write programs to handle text streams, because that is a universal interface.

Combine small, sharp tools and the use of a common underlying format — the line-oriented, plain text file — to accomplish larger tasks.

Unix Tools

Manipulate Files

  • cpcopy files
  • mvmove (rename) files
  • rmremove (delete) files
  • mkdirmake new directory
  • rmdirremove (delete) directory

Read Contents

  • catconcatenate and print file
  • headprint first part (head) of file
  • tailprint last part (tail) of file
  • lesspaginate through file (less is opposite of more)

Chaining

  • <cmd1> ; <cmd2>run cmd1, then run cmd2
  • <cmd1> && <cmd2>run cmd1, on success run cmd2 (logical AND)
  • <cmd1> || <cmd2>run cmd1, on failure run cmd2 (logical OR)

Example


                ~ $ mkdir dir1 && cd dir1 && mkdir dir2 && cd dir2 && echo "Success!"
                Success!
                ~/dir1/dir2 $ _
            
Create folder dir1 and then change into that folder and then create folder dir2 and then change into that folder and then print "Success!". Abort at the first failure.

Pipelines (1/3)

  • wcword count

Input
"one two"

Command
wc

Output
2

echo "one two" | wc -w

Pipelines (2/3)

Command 1

Command 2

Pipelines (3/3)

  • <cmd1> | <cmd2>use output of cmd1 as input for cmd2

Example


                ~ $ ls -1 *.php | wc -l
                42
                ~ $ _
            
List all of the files with the extension 'php' in single lines and count these lines.

Substitution (1/2)

  • echo Current folder is <XYZ>.=> Current folder is <XYZ>.
  • pwd=> /Users/alain
  • echo Current folder is pwd.=> Current folder is /Users/alain.

Substitution (2/2)

  • <cmd1> $(<cmd2>)use output of cmd2 as argument to cmd1

Example


                ~ $ echo "Currently in folder $(pwd)."
                Currently in folder /Users/alain.
                ~ $ _
            
Fetch the current working directory and use it to print a feedback message.

Composability

  • Mix-and-match commands to get the result you need.

  • Install additional tools if the provided ones are not sufficient.

  • Simplify workflows and automate work by building custom scripts.

  • Mix-and-match custom scripts to build entire automated systems.

Scripting (1/2)

Shell script: text file with one or more shell commands that are executed in a linear order.

Comment: piece of text that will not be executed and can be used to leave notes.

Shebang: Comment in first line of script to let the shell know about the interpreter to use.

File my-first-script.sh:


		                #!/bin/sh

		                # This is a comment.

		                # Run two commands in sequence.
		                echo "Hello World!"
		                echo "How are you doing?"
		            

Scripting (2/2)

File my-first-script.sh:


		                #!/bin/sh

		                # This is a comment.

		                # Run two commands in sequence.
		                echo "Hello World!"
		                echo "How are you doing?"
		            

 


                        $ my-first-script.sh
                        Hello World!
                        How are you doing?
                        $ _
                         
                         
                         
		            

WP-CLI

Goal

WP-CLI is the official command line tool for interacting with and managing your WordPress sites.

  • Goal: Fastest way to do anything with WordPress

Installation

Refer to the handbook, which starts with the section Installing .

Getting Help

  • wp helpGeneral help screen with a list of commands *
  • wp help <command>Help screen for a specific command

* The output might run through a "pager" like more to let you paginate. Hit q to exit that pager.

Global Parameters

  • --path=<path>Path to the WordPress files
  • --skip-plugins[=<plugin>]Skip loading all or some plugins
  • --prompt[=<assoc>]Prompt the user to enter values for the arguments
  • --quietSuppress informational messages
  • --debug[=<group>]Show all PHP errors and verbose bootstrap output

Framework commands

Managing WP-CLI

  • wp cli infoPrint details about the WP-CLI environment
  • wp cli updateUpdate WP-CLI to the latest release

Managing WP-CLI Packages

  • wp package listList installed WP-CLI packages
  • wp package install <package>Install a WP-CLI package
  • wp package uninstall <package>Uninstall a WP-CLI package
  • wp package updateUpdate all installed WP-CLI packages

Notable Bundled Commands

  • wp coreManipulate a WordPress install
  • wp dbPerform basic database operations
  • wp pluginManage plugins
  • wp themeManage themes
  • wp mediaManage attachments
  • wp postManage posts
  • wp userManage users
  • wp commentManage comments
  • wp scaffoldGenerate code for various components
  • wp search-replaceSearch/replace strings in the database
  • ...

Combining Commands

  • xargs <command>Split input into multiple calls to <command>

Example


                ~ $ wp cap list 'editor' | xargs wp cap add 'author'
                Success: Added 24 capabilities to 'author' role.
                ~ $ _
            
List all the capabilities for the role editor and then pass these on to the next command to add these capabilities to the role author.

Common Use Cases

Create a new WordPress installation


                    Sites $ wp core download --path=newsite
                    Creating directory '/Users/alain/Sites/newsite/'.
                    Downloading WordPress 4.7.4 (en_US)...
                    md5 hash verified: eefcac6dcb954183fff27e5998d62b5f
                    Success: WordPress downloaded.
                    Sites $ cd newsite
                    newsite $ wp core config --dbname=newsite --dbuser=root
                    Success: Generated 'wp-config.php' file.
                    newsite $ wp db create
                    Success: Database created.
                    newsite $ wp core install --url=newsite.dev --title="New Site"
                              --admin_user=mr_robot --admin_email=ea@evilcorp.com
                    Admin password: bAgvzQlmNsYY
                    Success: WordPress installed successfully.
                    newsite $ _
                

Run all updates


                    newsite $ wp core update
                    [ ... ]
                    Success: WordPress updated successfully.
                    newsite $ wp plugin update --all
                    [ ... ]
                    Success: Updated 4 of 4 plugins.
                    newsite $ wp theme update --all
                    [ ... ]
                    Success: Updated 1 of 1 themes.
                    newsite $ _
                

Regenerate thumbnails


                    newsite $ wp media regenerate --yes
                    Found 2 images to regenerate.
                    1/2 Regenerated thumbnails for "cropped-Banner.png" (ID 15).
                    2/2 Regenerated thumbnails for "Banner" (ID 14).
                    Success: Regenerated 2 of 2 images.
                    newsite $ _
                

What's Next ?

Learn — WP-CLI Handbook

Discuss — WP-CLI Slack Channel

Contribute — WP-CLI GitHub Organization

Questions ?

I'm Alain Schlesser.


Follow me on Twitter:

  @schlessera

Or visit my Personal Blog:

   www.alainschlesser.com