Tutorials WordPress

Automate WordPress Management Tasks with WP-CLI

Written by Caleb Boylan

This is the first in a series of tutorials designed to help make your WordPress efforts more easily manageable by using WP-CLI.  

WordPress Command Line Interface, or WP-CLI, is a simple but powerful command line tool that when used right, can simplify most tasks required to run a successful WordPress website. For example, with WP-CLI you can upgrade “WordPress core” or install a plugin with a single command, no logins and clicks necessary! Your WordPress site will feel like a well-oiled machine.

wp

In this series of blog posts, we will cover the basic but useful tasks with WP-CLI such as upgrading WordPress core and managing your plugins. By the end of it, you’ll be comfortable using WP-CLI and discovering even more ways you can use it to simplify your life!

Installing and configuring WP-CLI

If you are using DreamPress or have WordPress installed on a shared, VPS, or dedicated hosting account with us, you can just log in to the server and you’ll find WP-CLI waiting for your commands. Login to your remote server and check it out:

[user@dreamhost-server]$ wp --info

WP-CLI needs the path where the WordPress installation lives to get started. The easiest way is by providing the path in the command line. For example, if you have one WordPress site installed in /home/user/domain, the command to check WordPress version should be:

[user@server] $ wp --path=/home/user/domain core version

If you only run one domain, you can enter the path in a YAML file, storing path and all the most used options there. Add this line to your ~/.wp-cli/config.yml config file:

path: “/home/username/domain”

After you have added this to your config file, run the following to confirm your path is correct:

[user@server]$ wp core version

This shows the version number of your WordPress install. If it succeeds, your path is configured correctly; if it does not, check your path again to make sure there are no typos.

If you manage multiple WordPress sites, it might be easiest to install WP-CLI locally and configure it to manage multiple sites. If you are on MacOS and have brew installed, you can use it to install wp-cli using the following:

[user@localhost] $ brew install wp-cli

Otherwise, install PHP, then download WP-CLI so you can run it locally.

[user@localhost] $ curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

[user@localhost] $ chmod +x wp-cli.phar

[user@localhost] $ ln -s wp-cli.phar wp

Make sure WP-CLI can run with the following:

[user@localhost] $ ./wp --info

Now you need to configure WP-CLI to manage multiple sites, using the ~/.wp-cli/config.yml config file. For example, let’s assume you have a production website on a server named prod.example.com with WordPress, in the path /home/example/prod.example.com; you also have a staging site at staging.example.com with WordPress in the path /home/example/staging.example.com and each site’s ssh user is example. In the YAML configuration file you’ll create two alias entries, @prod and @staging like in the example below:

@prod:

    user: example

    ssh: prod.example.com

    path: “/home/example/prod.example.com”
@staging:

    user: example

    ssh: staging.example.com

    path: “/home/example/staging.example.com”

Then when you want to run commands against the production site, you would use the @prod aliases to pass commands to the appropriate server:

[user@localhost] $ ./wp @prod core version

And to reach the staging server, use @staging alias:

[user@localhost] $ ./wp @staging core version

If you manage production and staging WordPress sites for multiple customers, you can see how handy that is—for example, you can upgrade all WordPress sites to a new release from your own computer, no extra clicks necessary!

In the next episode, we’ll show you how to manage your WordPress core version using a handy terminal application. Stay tuned!

Have questions? Leave a comment below, or chat with us on Twitter @DreamHostCare!

About the author

Caleb Boylan

Caleb is the Developer Advocate for the Corporate Communications team at DreamHost.