Introduction to WP-CLI

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:

bash
curl -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

bash
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
bash
wp core download$
  1. Create wp-config.php file
bash
wp config create --dbname=DB_NAME --dbuser=DB_USER --dbpass=DB_PASS$
  1. Install WordPress
bash
wp 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:

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

bash
wp core update$

Managing plug-ins

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

bash
wp plugin install hello-dolly$

And activate it:

bash
wp plugin install hello-dolly --activate$

Or do it in two separate steps:

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

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

bash
wp plugin deactivate hello-dolly$

Or turn it off and delete entirely:

bash
wp plugin uninstall hello-dolly --deactivate$

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

bash
wp plugin list$

Updating plug-ins

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

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

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

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

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

bash
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

bash
wp theme install twentytwenty $ wp theme activate twentytwenty$$

Or use a single command:

bash
wp install twentytwenty --activate$

To delete a theme, run

bash
wp theme delete twentytwenty$

To update all themes at once, use

bash
wp theme update --all$

And to list them, run

default
$ wp theme list
Tip
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

bash
wp user list --all$

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

bash
wp user list --role=administrator$

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

bash
wp user create buddy email@test.com --role=author --user_pass=some-password$
Tip
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:

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

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

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

Or if you want to generate a specific size:

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

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

Or you can import an image from an external server:

bash
wp media import https://example.com/images/test.jpg$

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

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

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

bash
wp search-replace 'http://example.test' 'http://example.com' --all-tables-with-prefix$
Tip
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:

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

Success
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: Image loading...Example 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.