Introduction to WP-CLI
WP-CLI is the perfect tool to manage WordPress from the terminal. You can use it to install themes and plug-ins, update resources, and manage users. This guide will walk you through the most useful commands and will show you how to use them in practice.
Installing WP-CLI on server
WP-CLI is most often run remotely via SSH. To install the CLI, run these commands on your server:
bashcurl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar chmod +x wp-cli.phar mv wp-cli.phar /usr/local/bin/wp
$$$
To check if the installation was successful, run
bashwp --info
$
Now you can run WordPress commands. Let's make a tour of the most useful ones.
Installing WordPress
Follow these steps to install WP from the command line level:
- Download installation files
bashwp core download
$
- Create
wp-config.php
file
bashwp config create --dbname=DB_NAME --dbuser=DB_USER --dbpass=DB_PASS
$
- Install WordPress
bashwp core install --url=wordpress.site --title="WordPress site title" --admin_user=admin_login --admin_password=admin_pass --admin_email=admin@example.com
$
If you don't want to enter the password in a direct way, you can use the prompt
parameter:
bashwp core install --url=wordpress.site --title="WordPress site title" --admin_user=admin_login --admin_email=admin@example.com --prompt=admin_password < admin_password.txt
$
Updating WP Core
You know how to install WordPress. This command will help you make sure it's up-to-date:
bashwp core update
$
Managing plug-ins
WL-CLI lets you install any WordPress plug-in:
bashwp plugin install hello-dolly
$
And activate it:
bashwp plugin install hello-dolly --activate
$
Or do it in two separate steps:
bashwp plugin install hello-dolly wp plugin activate hello-dolly
$$
If you no longer need a plug-in, you can turn it off using
bashwp plugin deactivate hello-dolly
$
Or turn it off and delete entirely:
bashwp plugin uninstall hello-dolly --deactivate
$
You can check what plug-ins you have installed with the list
command:
bashwp plugin list
$
Updating plug-ins
WP-CLI lets you easily update existing plug-ins:
bashwp plugin update hello-dolly
$
The command comes with three useful parameters: --dry-run
, --all
, and --minor
.
The --dry-run
parameter will let you list the plug-ins that require and update without actually running the process:
bashwp plugin update --all --dry-run
$
--all
will force an update on all plug-ins:
bashwp plugin update --all
$
--minor
, on the other hand, will only allow lesser updates, for example 1.0 to 1.1, but not 1.0 to 2.0.
bashwp plugin update --all --minor
$
Managing themes
Theme management is basically the same as managing plug-ins – you just need replace plugin
with theme
.
To install and activate a theme, run
bashwp theme install twentytwenty $ wp theme activate twentytwenty
$$
Or use a single command:
bashwp install twentytwenty --activate
$
To delete a theme, run
bashwp theme delete twentytwenty
$
To update all themes at once, use
bashwp theme update --all
$
And to list them, run
default$ wp theme list
--dry-run
and --all
parameters. Be aware, though, that --minor
doesn't work when updating themes.
Managing users
WP-CLI also offers a couple of commands to manage you co-workers. To display the list of users added to your WP accountr, run
bashwp user list --all
$
To restrict the list to admins only, add the --role
parameter:
bashwp user list --role=administrator
$
To add a user with a given role, enter the following:
bashwp user create buddy email@test.com --role=author --user_pass=some-password
$
administrator
, editor
, author
, contributor
, subscriber
. You can read about the roles here.
To edit an existing user, use update
:
bashwp user update buddy --user-pass=new-password --first_name="new name"
$
Managing images and media
The commands below will come in handy during migration or rebuilding a website from scratch.
The wp media regenerate
command lets you regenerate images on your website:
bashwp media regenerate --yes
$
If you added new media dimensions to your theme, you can generate only the missing image sizes by adding a parameter to the command:
bashwp media regenerate --yes --only-missing
$
Or if you want to generate a specific size:
bashwp media regenerate --yes --image_size=new_image_size
$
Another very useful command is wp media import
. It allows you to import images from the selected directory on the server:
bashwp media import ~/Pictures/**\/*.jpg
$
Or you can import an image from an external server:
bashwp media import https://example.com/images/test.jpg
$
You can also import the image and immediately set it up as featured:
bashwp media import https://example.com/images/test.jpg --featured_image --post_id=123
$
Search and replace in database
Whenever migrating something in WordPress, wp search-replace
proves indispensable. Besides using it to find and replace text, it copes really well with serialized data, a format often used by WordPress to store data.
For example, to change the domain, just run
bashwp search-replace 'http://example.test' 'http://example.com'
$
The command above will replace the domain in all default WordPress tables. However, if you'd like to replace it only in tables of a specific prefix, you need to add a parameter:
bashwp search-replace 'http://example.test' 'http://example.com' --all-tables-with-prefix
$
And finally, what if we owant to replace the domain in wp_posts
only? Use this:
bashwp search-replace 'http://example.test' 'http://example.com' wp_posts
$
Summary
The operations described here are just a fragment for what WP-CLI can help you achieve. The full list of commands is available here. Adding Buddy will let you automate many of them and save loads of time.
Stay tuned for more on WordPress optimization with Buddy CI/CD!
Maciek Palmowski
WordPress & PHP Developer
Maciek is a WordPress Ambassador working at Buddy. An active participant in the Open Source world, he spends most of his free time trying to find interesting news for WP Owls newsletter or cycling.