Start – Getting things in place
First up, you’ll need SSH access and navigate to the directory where your WordPress install lives. This can vary greatly by the host, though should be visible within your hosting control panel.
Let’s say that my web root is /www/ed/public_html – many times you can there or close by running cd ~
You can always run pwd
to see the current directory you’re working in.
Remember, we’re looking for our WordPress file structure. Find this by using ls
– you should see a list of files and directories that include:
wp-activate.php
wp-admin
wp-blog-header.php
wp-comments-post.php
wp-config.php
wp-config.php.save
wp-config-sample.php
wp-content
wp-cron.php
wp-includes
wp-links-opml.php
wp-load.php
wp-login.php
wp-mail.php
wp-settings.php
wp-signup.php
wp-trackback.php
xmlrpc.php
Let’s make sure that WP CLI is installed and available and installed correctly:
wp cli info
– gives a breakdown of the configuration.
wp help
will bring up the CLI’s help screen which can show you which commands it’s capable of as well as arguments, flags, etc.
If you’re using a VPS or un-managed server, you’ll likely need to switch to a user without root privileges, WP CLI will let you know about this as well: do not run WP CLI commands in root.
Interacting with WordPress
Now that we know everything is in place to use WP CLI, let’s fire some commands.
wp core
– simply outputs the current WordPress version
wp plugin
– displays all plugins installed (active and inactive)
wp theme
– same as above, but for themes
wp maintenance-mode
– allows you to enable or disable the built-in WP maintenance mode. This can be useful if you need to quickly render your site unaccessible, or if you get the stuck maintenance message while updating WP things. wp maintenance-mode activate
to deactivate, replace activate with deactivate
wp option
– quickly view/add/update WP database options. example: wp option get site_url
– you can use wp option set
to update any option value.
wp db
– more general database options
wp search-replace
– yet another database option, this allows you to replace instances of text
wp network
– yes, it supports WP multisite networks as well. (we’ll dive deeper into this in it’s own article)
wp super-admin
– quickly add multisite super admin privileges to a WP user
wp config
– generate a new wp-config.php file, or even use the tool to create new salts with wp config shuffle-salts
wp server
– really cool for local environments, it uses PHP to create a web server on localhost
Flags and subcommands
Nearly every command has a similar list of subcommands, this includes: list
, add
, install
, activate
, delete
, update
Sometimes we’re drawn to WP CLI when the site we’re working on is experiencing issues, most commonly 500/PHP Fatal errors. The specific issue really depends on what parts of WP CLI will throw errors, but we do have some options with the skip flags. If a plugin or the theme is causing the issue, this should work: --skip-themes --skip-plugins
Show me some examples!
I’ve got other posts that cover the wonders of WP CLI more up close, but we’ll be showing quick examples here for many of the commands listed above.
Plugins
wp plugin install woocommerce
– install WooCommerce
wp plugin activate woocommerce
– activate it
Or we could have activated it on install: wp plugin install woocommerce --activate
wp plugin list
– show all installed plugins
wp plugin deactivate woocommerce
– deactivate it
The wp themes
command is used just like the plugin commands.
Database/options
wp db search "stuff"
search the database for the word stuff
wp search-replace "stuff" "things"
runs a search and replace, all instances of stuff will now be things
wp option update siteurl "https://mydomain.com
” updates the siteurl option, don’t forget about home as well!
Core
wp core download --skip-content --force
force install WordPress core, leaving wp-content
directory alone
wp core version
check the WordPress core version
wp core verify-checksums
verify core checksums, ensure no files have been added, removed, or modified