WP-CLI is a command line interface for WordPress that allows users to manage WordPress sites and perform administrative tasks faster through commands. The document discusses how to install and configure WP-CLI, commonly used commands that mirror the WordPress graphical user interface, commands for working behind the scenes of WordPress, using WP-CLI for configuration, scaffolding plugins and themes, working with databases, and writing custom commands.
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)
10. 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/
11. 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
13. 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
15. Core
Check for available core update
$ wp core check-update
Update WordPress
$ wp core update
Convert to Multisite
$ wp core multisite-convert
Install Multisite
$ wp core multisite-install
--title="Welcome to the
WordPress"
> --admin_user="username"
--admin_password="password"
> --admin_email="user@email.com"
https://developer.wordpress.org/cli/commands/core/
16. Plugins
List plugins
$ wp plugin list
Install and activate a plugin from wp plugin repository
$ wp plugin install akismet –-activate
Update plugins
$ wp plugin update akismet
$ wp plugin update akismet wp-super-cache woocommerce
$ wp plugin update --all
https://developer.wordpress.org/cli/commands/plugin/
17. 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/
18. Combine Commands with Pipes
Update core, all plugins, and all themes
$ wp core update | wp plugin update --all | wp theme
update --all
19. Users
List users
$ wp user list
Create user
$ wp user create ann ann@example.com --porcelain --send-email
Delete user
$ wp user delete john --reassign=lynn
$ wp user delete john --yes
https://developer.wordpress.org/cli/commands/user/
20. 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/
24. 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/
25. 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/
28. 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/
29. 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/
30. 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/
31. 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/
40. 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/
50. Database
Create database
$ wp db create
Import database
$ wp db import wpdb.sql
Export database
$ wp db export
Reset current database
$ wp db reset --yes
Delete existing database
$ wp db drop --yes
https://developer.wordpress.org/cli/commands/db/
51. 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/
52. 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
53. 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/
54. 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/
58. 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/
67. 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/
68. 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
1. Intro
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
Except uploading files, anything in the GUI in the CLI
We have this GUI…
1a. Compare GUI & CLI
b. Bundle tasks together
c. Use or write packages
2a. Plugin, phpMyAdmin, hosting control panel
b. Not switching taking up your time
1. Windows support To borrow a phrase, “UNIX is a requirement that begins where requirement ends.”
4. Choice of terminal apps.
Full-service hosting,
Good news.
Bad news.
a. Non-UNIX servers have own installations
b. Can also install with Git, Composer, Homebrew, Docker
If you’re new to command line, good place to start.
Get a feel for executing commands
Learn how to know where you are when you can’t see your whole tree at once
Won’t break anything.
EXAMPLES
If you know WP, you know what these commands do. Low bar to entry. WP-CLI teaches you itself.
Appreciate Hierarchical structure: wp = tool, command = area, subcommand =what you’re doing
Flavor Efficiency: Compare examples with GUI
Argument coming into play
List includes names, status (activated), update availability, version
2. Like with themes…
3. As we’ve seen, update…
2. String together commands you want to run after we’ve created a new WP install
Here we are passing data from one command to another. List of status=spam in the form of ids
4. Last line to keep password from logging in bash history.
** What I was saying before about full-service hosts…
You may not be able to create db or admin_password to txt file
Your mileage may vary
? Anyone know of full-service host that allows creating db at CL
Note: ID, username or email.
Plugins remove load and security
1. And assign capabilities as previous
2. Delete role reverts user to previous role
3. Reset changes to default capabilities
***Themes, plugins, blocks, post types, taxonomies
Child theme: includes style.css, function.php (enqueues scripts and styles), .editorconfig
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
Things I’m leaving out so there are some treasures you can find
WC-CLI
Create staging/production pairs
Create staging group and production group
2. Update core, all themes, all plugins on all aliases
Command line configuration is stored in config.yml
2. & 3. files inside the current working directory (or upwards)
Fine-tune config to your needs
But that’s not all
***LIST!!!
***LIST
Diagnoses problems within WordPress.
Checking security, performance, configuration
**Helps identify where WordPress is slow.
With spotlight, we’re getting four hooks of interest
Try your hand
We have a Scaffold Package command
Keep track of your author/packagename to find it