WP-CLI is the official command line tool for interacting with and managing your WordPress sites. Learn to use it to speed up maintenance and deployment tasks, and to aid in theme and plugin development.
3. Agenda
1. WP-CLI: What and Why
2. Getting Started
3. Commands
a. Mirroring the GUI
b. Behind the Scenes
4. Configuration
5. Packages
6. Writing Commands
4. What isWP-CLI?
WP-CLI is the official command line
interface for WordPress.
What is a command line interface?
A command-line interface…is a means of
program where the user issues commands to the
of successive lines of text. (Wikipedia)
11. WP-CLI Installation
1) Download wp-cli.phar
$ curl -O https://raw.githubusercontent.com/wp-cli/builds/
gh-pages/phar/wp-cli.phar
2) Confirm it works
$ php wp-cli.phar --info
3) Enable use of wp instead of php wp-cli.phar
$ chmod +x wp-cli.phar
$ sudo mv wp-cli.phar /usr/local/bin/wp
4) Confirm successful installation
$ wp --info
https://make.wordpress.org/cli/handbook/installing/
12. Navigation
pwd print working directory
ls list segments
(non-OS contents)
cd change directory
../ parent directory
/ system root
- last directory
~ home directory
Up and down arrow keys to review and
execute previous commands
Tab to autocomplete options (installation
required)
https://wp-cli.org/#tab-completions
14. Global Parameters
Perform operation against a remote server over SSH
--ssh=[<scheme>:][<user>@]<host|container>[:<port>][<path>]
Path to the WordPress files
--path=<path>
Suppress informational messages
--quiet
Prompt the user to enter values for all arguments
--prompt
https://make.wordpress.org/cli/handbook/config/#global-parameters
19. Themes
List themes
$ wp theme list
Install and activate a theme from wp theme repository
$ wp theme install twentynineteen --activate
Update themes
$ wp theme update twentynineteen
$ wp theme update twentynineteen twentyseventeen twentysixteen
$ wp theme update --all
https://developer.wordpress.org/cli/commands/theme/
20.
21. Combine Commands with Pipes
Update core, all plugins, and all themes
$ wp core update | wp plugin update --all | wp theme
update --all
22. Users
List users
$ wp user list
Create user
$ wp user create ann ann@example.com --porcelain --send-email
https://developer.wordpress.org/cli/commands/user/
23. Users
Reset user password
$ wp user reset-password john
$ wp user reset-password john@example.com ann@example.com
$ wp user reset-password 1 2 3
Delete user
$ wp user delete john --reassign=lynn
$ wp user delete john --yes
https://developer.wordpress.org/cli/commands/user/
24. Options
List site options
$ wp option list
Change site URL
$ wp option update siteurl http://new.siteurl.com
Change admin email
$ wp option update admin_email admin@siteurl.com
https://developer.wordpress.org/cli/commands/option/
25. Options
Change image thumbnail size
$ wp option update thumbnail_size_w 200 | wp option update
thumbnail_size_h 200 | $ wp media regenerate --yes
Block search engines, change site description and time zone,
close commenting
$ wp option update blog_public 0 | wp option update
blogdescription "My New Tagline" | wp option update gmt_offset
-7 | $ wp option update default_comment_status closed
https://codex.wordpress.org/Option_Reference/
26. Comments
List comments
$ wp comment list
Approve comment
$ wp comment approve 1337
Delete all spam comments
$ wp comment delete $(wp comment list --status=spam
--format=ids)
https://developer.wordpress.org/cli/commands/comment/
28. Site for Multisite Installations
List sites
$ wp site list
Create a site
$ wp site create --slug=example
Deactivate a site
$ wp site deactivate 123
Archive a site
$ wp site archive 123
https://developer.wordpress.org/cli/commands/site/
29. SuperAdmin
List super admins
$ wp super-admin list
Add a super admin
$ wp super-admin add username
Remove a super admin
$ wp super-admin remove username
https://developer.wordpress.org/cli/commands/super-admin/
33. User Capabilities
List a user’s capabilities
$ wp user list-caps 21
Add a capability to user
$ wp user add-cap johnsmith edit_product
Remove a capability to user
$ wp user remove-cap john@smith.com publish_newsletters
https://developer.wordpress.org/cli/commands/user/
34. Role Capabilities
List the capabilities of a role, sorted alphabetically
$ wp cap list 'author' | sort
Add a capability to a role
$ wp cap add author spectate
Remove a capability from a role
$ wp cap remove author spectate
https://developer.wordpress.org/cli/commands/cap/
35. Roles
Create a role
$ wp role create productadmin "Product Administrator"
Delete a role
$ wp role delete productadmin
Reset roles
$ wp role reset administrator
$ wp role reset administrator author contributor
$ wp role reset --all
https://developer.wordpress.org/cli/commands/role/
36. Posts and Pages
Generate posts
$ wp post generate --count=10
Generate pages with lorem ipsum
$ curl http://loripsum.net/api/5 | wp post generate
--post_type=page --count=10
https://developer.wordpress.org/cli/commands/post/
46. Scaffold PostTypes
Generate custom post type
$ wp scaffold post-type my-post-type-slug --label="My Post
Type" --textdomain="my-post-type"
Generate custom post type for a theme
$ wp scaffold post-type my-post-type-slug --label="My Post
Type" --theme=my-theme
Generate custom post type for a plugin
$ wp scaffold post-type my-post-type-slug --label="My Post
Type" --plugin=my-plugin
https://developer.wordpress.org/cli/commands/scaffold/post-type/
57. Database
Create database
$ wp db create
Reset current database
$ wp db reset --yes
Delete existing database
$ wp db drop --yes
https://developer.wordpress.org/cli/commands/db/
58. Site
Remove pages, posts, and custom post types
$ wp site empty --yes
Remove pages, posts, custom post types, and uploads
$ wp site empty --uploads --yes
https://developer.wordpress.org/cli/commands/site/
59. Database
Import database
$ wp db import wpdb.sql
Export database
$ wp db export
Open a MySQL console
$ wp db cli
https://developer.wordpress.org/cli/commands/db/
60. DatabaseWith mysqlcheck
Check database
$ wp db check
Optimize database
$ wp db optimize
Repair database
$ wp db repair
https://dev.mysql.com/doc/refman/8.0/en/mysqlcheck.html
61. Config
Create wp-config.php
$ wp config create --dbname=database-name --dbuser=db-user
--dbpass=db-password --dbhost=hostname --dbprefix=prfx_
Set constants and variables
$ wp config set WP_DEBUG true --raw
Change salt keys
$ wp config shuffle-salts
https://developer.wordpress.org/cli/commands/config/
62. Search and Replace
Search for staging URL and replace with production URL
$ wp search-replace 'http://example.dev' 'http://example.com'
--dry-run
Search/replace to a SQL file without transforming the database
$ wp search-replace 'http://example.dev' 'http://example.com'
--export=database.sql
Run case-insensitive regex search/replace operation
$ wp search-replace '[foo id="([0-9]+)"' '[bar id="1"' --
regex --regex-flags='i'
https://developer.wordpress.org/cli/commands/search-replace/
66. WP-Cron
List scheduled cron events
$ wp cron event list
Schedule a new cron event
$ wp cron event schedule cron_test
Test that cron runs successfully
$ wp cron test
https://developer.wordpress.org/cli/commands/cron/
78. Packages
List installed packages
$ wp package list
Install a package
$ wp package install package-slug
Update a package
$ wp package update package-slug
Uninstall a package
$ wp package uninstall package-slug
https://developer.wordpress.org/cli/commands/package/
79. Doctor
Install
$ wp package install git@github.com:wp-cli/doctor-command.git
Get list of checks wp doctor can perform
$ wp doctor list
Perform checks
$ wp doctor check core-verify-checksums
$ wp doctor check core-verify-checksums file-eval
php-in-uploads
$ wp doctor check --all
https://github.com/wp-cli/doctor-command
BEFORE WE START: Who is familiar with command line? Who is using WP-CLI?
1) Lot of info
Slides
2) Firehose
Helicopter tour
Scope
Points of interest
Professionally for 11 years
Started with content and design, moving into development
where I’m working happily these days.
1. Background
2. Basic Installation
3. Bulk of preso
4. Not required by helpful
5. Go over a couple packages to extend WP-CLI.
6. Touch on Writing Commands
Stand-alone replacement for the GUI
Anything in the GUI in the CLI, except uploading files
We have this GUI…
1.
a. Replaces GUI interactions with no browser refresh
b. Bundle tasks together
c. Use or write packages
2.
a. Plugin, phpMyAdmin, hosting control panel
b. Not switching taking up your time
Requirements, Installation, Navigation, Syntax, Global parameters
1. Windows support To borrow a phrase, “UNIX is a requirement that begins where requirement ends.”
4. Choice of terminal apps. PuTTY GUI
Full-service hosting,
Good news.
Bad news: Doesn’t mean they support everything
***non-UNIX servers or Git, Composer, Homebrew, Docker are supported and require their own approaches.
Tight rope > balance beam
If you’re new to command line, good place to start.
Get a feel for executing commands
Learn how to navigate when you can’t see everything at once
Won’t break anything.
EXAMPLES
“If you know WP, you know what these commands do”
Appreciate Hierarchical structure: wp = tool, command = area, subcommand =what you’re doing
Flavor Efficiency: Compare examples with GUI
Argument coming into play
We also have global parameters to help us along
--prompt, requires read times, cut the keystrokes in half
1. List includes names, status (activated), update availability, version
1. List includes names, status (activated), update, version
2. As we’ve seen
3.
We’ve learned 3 commands.
Might want to start combining
One command to update all the thing
1. username, display name, email, reg date/time, role
2. --porcelain suppressed password, so it doesn’t end up in your bash history
2. String together a bunch of commands you want to run after we’ve created a new WP install
WAIT Add a post, edit a page, add a widget to sidebar, assign a menu to location
I’ll leave some of those things for your own discovery.
4. Last line to keep password from logging in bash history.
What I was saying before about full-service hosts
5. Note May not be able to create db with full-service hosting.
6. Your mileage may vary
***Anyone know of full-service host that allows creating db at CL
*** LIST
** Assign capabilities, as previous
2. Delete role reverts user to previous role
3. Reset remove changes to capabilities
Format-ids not required, but with get multiple error messages
So, within areas we ordinarily work with in the GUI, we have deeper capabilities in the CLI
We also have commands not associated with GUI commands
***Themes, plugins, blocks, post types, taxonomies
Child theme: includes style.css, function.php (enqueues scripts and styles), .editorconfig
Registering post type > split to make image more readable
Registering post type > split to make image more readable
Creating custom messages
Handy things for theme and plugin dev
Reset removes all tables, sets back to pre-install
Drop deletes the db
Use mysqlcheck utility to perform table maintenance
Outputs results per table
Check checks for errors
Optimize optimizes (incl. --optimize=true)
Repair can fix almost anything except unique keys that aren't unique. (incl. --repair=true)
As we’ve seen
… without opening a file
S/R will work on serialized data, but not inside a serialized object
List
Update
Think about Flush
Flush your blessed cache
No, flush will not on CloudFlare
List: shows hook, time (local and GMT), and recurrence rate
That’s a peek behind the scenes.
1. WC-CLI
That’s the beginning
Long commands, you may want more efficiency.
Early point of verbose commands.
1) Create pairs
***@ALL alias When you’re doing this WP-CLI is setting up an @all alias
Now you’re really updating all the things
Or one command to rule them all
Dare I say one command to update them all
Don’t let the power go to your head
4. Where command line configuration is stored in config.yml
2. + 3. file inside the current working directory (or upwards)
so you can fine-tune config to your needs
While we’re talking about being efficient
***LIST!!!
***LIST
Diagnoses problems within WordPress.
Checking security, performance, configuration
**Helps identify where WordPress is slow.
Look for target page for stage??
Look up hook for specifics
Remind yourself #2
With spotlight, we’re getting four hooks of interest
Used to be a repo for packages
Check GitHub
If you are inspired…
Try your hand
We have a Scaffold Package command
Keep track of your author/packagename to find it