Introduction to WP-CLI

November 25, 2020


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:

curl -O
chmod +x wp-cli.phar
mv wp-cli.phar /usr/local/bin/wp$$$

To check if the installation was successful, run

wp --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:

  1. Download installation files
wp core download$
  1. Create wp-config.php file
wp config create --dbname=DB_NAME --dbuser=DB_USER --dbpass=DB_PASS$
  1. Install WordPress
wp core install --title="WordPress site title" --admin_user=admin_login --admin_password=admin_pass$

If you don't want to enter the password in a direct way, you can use the prompt parameter:

wp core install --title="WordPress site title" --admin_user=admin_login --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:

wp core update$

Managing plug-ins

WL-CLI lets you install any WordPress plug-in:

wp plugin install hello-dolly$

And activate it:

wp plugin install hello-dolly --activate$

Or do it in two separate steps:

wp plugin install hello-dolly
wp plugin activate hello-dolly$$

If you no longer need a plug-in, you can turn it off using

wp plugin deactivate hello-dolly$

Or turn it off and delete entirely:

wp plugin uninstall hello-dolly --deactivate$

You can check what plug-ins you have installed with the list command:

wp plugin list$

Updating plug-ins

WP-CLI lets you easily update existing plug-ins:

wp 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:

wp plugin update --all --dry-run$

--all will force an update on all plug-ins:

wp 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.

wp 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

wp theme install twentytwenty
$ wp theme activate twentytwenty$$

Or use a single command:

wp install twentytwenty --activate$

To delete a theme, run

wp theme delete twentytwenty$

To update all themes at once, use

wp theme update --all$

And to list them, run

$ wp theme list
You can also add --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

wp user list --all$

To restrict the list to admins only, add the --role parameter:

wp user list --role=administrator$

To add a user with a given role, enter the following:

wp user create buddy --role=author --user_pass=some-password$
Here are the roles you can assign: administrator, editor, author, contributor, subscriber. You can read about the roles here.

To edit an existing user, use update:

wp 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:

wp 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:

wp media regenerate --yes --only-missing$

Or if you want to generate a specific size:

wp 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:

wp media import ~/Pictures/**\/*.jpg$

Or you can import an image from an external server:

wp media import$

You can also import the image and immediately set it up as featured:

wp media import --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

wp search-replace 'http://example.test' ''$

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:

wp search-replace 'http://example.test' '' --all-tables-with-prefix$
If you want to update all tables in the database, use just --all-tables.

And finally, what if we owant to replace the domain in wp_posts only? Use this:

wp search-replace 'http://example.test' '' wp_posts$


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.

For example, you can configure a pipeline that will perform a full back of your WP instance and upload it on the backup server every day: Example configExample config

Stay tuned for more on WordPress optimization with Buddy CI/CD!

Maciek Palmowski

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.

With Buddy even the most complicated CI/CD workflows take minutes to create

Sign up for Buddy CI/CD

Start a free trial