Harness the Power of WP-CLI to Manage Your WordPress Sites
WordPress is well known for being very user-friendly, and that is one of the reasons it has become one of the most popular content management systems on the web. But when it comes to managing multiple WordPress sites and repeating the same operations over and over again it can become quite a tedious task for developers. And that brings us to WP-CLI, a set of command line tools to help speed up the development process and accomplish tasks more efficiently and quickly. Many WordPress professionals use tools like ManageWP or WP Remote; WP-CLI is a powerful alternative of these tools specifically for developers!
WP-CLI's mission is to be, quantitatively, the fastest interface for developers to manage WordPress.
What is WP-CLI?
WP-CLI is an open source set of command line tools to help make web developers lives easier when it comes to managing WordPress installations. You can install WordPress (including Multisite), update plugins, take backups, perform database operations, publish content, manage WP-Cron events, and perform hundreds of other tasks without ever touching your web browser. If you prefer working from the command-line then WP-CLI is your friend!
WP-CLI was originally created by Andreas Creten and Cristi Burcă who pushed their first source code to GitHub on November 06, 2011. In recent years there has been a lot of buzz around WP-CLI and according to Google trends the term "WP-CLI" is being searched more than ever before.
Daniel Bachuber, who has now maintained the project since 2014, is also pushing to unlock the potential of the WP REST API at the command line with his new RESTful WP-CLI project. He started a KickStarter campaign for RESTful WP-CLI and his $17,500 goal was completely funded within only 12 hours. That just goes to show you that the WordPress developer community is wanting to see more command line tools and support!
How to install WP-CLI
To get started with WP-CLI you need to ensure that you have SSH access to your web host or server. All modern providers like DigitalOcean, Linode, Vultr provide this and allow you to spin up a VPS in a matter of minutes. There are even managed WordPress hosts like Pantheon which give you free dev environments and support WP-CLI. Other requirements for WP-CLI are PHP 5.3.2 or later, WordPress, 3.4 or later, and a UNIX like environment. If you are on a Mac you can just dive right into the installation! If you happen to be on Windows, you can use command-line tool like Cygwin or a virtual machine. See David's awesome Automated WordPress Installation with Windows & WP-CLI. Follow the steps below to install WP-CLI.
In this example we are taking screenshots from a Windows WP-CLI setup.
SSH into your server.
Get the latest version of WP-CLI from GitHub using curl or wget. The following command will extra the WP-CLI files to root of your user directory.
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
You run the command below to check and make sure the file downloaded correctly.
php wp-cli.phar --info
It will then output like this:
Make it executable.
chmod +x wp-cli.phar
This is an optional step but we recommend moving the file to a folder so you can execute it from anywhere, and also give it an easy name like
wpso you can use the WP-CLI commands by typing it at the start.
sudo mv wp-cli.phar /usr/local/bin/wp
Here is a list of the basic commands you can use with WP-CLI. You can see a more in-depth explanation of each command here. Or you can type the following in the command-line to generate helpful information and the subcommands available.
|Manage the object cache.|
|Manage user capabilities.|
|Get information about WP-CLI itself.|
|Download, install, update and otherwise manage WordPress proper.|
|Manage WP-Cron events and schedules.|
|Perform basic database operations.|
|Execute arbitrary PHP code.|
|Load and execute a PHP file.|
|Export content to a WXR file.|
|Get help on WP-CLI, or on a specific. command.|
|Import content from a WXR file.|
|List, create, assign, and delete menus.|
|Manage post types.|
|Manage rewrite rules.|
|Manage user roles.|
|Generate code for post types, taxonomies, etc.|
|Search/replace strings in the database.|
|Launch PHP's built-in web server for this specific WordPress installation.|
|Interactive PHP console.|
|Perform site-wide operations.|
|List, add, and remove super admins from a network.|
|Manage sidebar widgets.|
There is also a list of known WP-CLI community commands used in popular third party plugins like BackupBuddy, MainWP, WP Migrate DB Pro, etc. And since it is open source you can even write your own WP-CLI commands.
WP-CLI how to's
Now that you have WP-CLI installed, there are literally hundreds of tasks you can accomplish and speed up using WP-CLI. Below we will discuss a few common examples.
Install WordPress with WP-CLI
Probably the most important one, how to install WordPress! We will be using the basic
First you need to download WordPress. You can download a certain version of WordPress by appending the following with the version number, example:
wp core download
Then you need to create your
wp-config.phpfile. Obviously this assumes you have already created the database.
wp core config --dbname=databasename --dbuser=databaseuser --dbpass=databasepassword --dbhost=localhost --dbprefix=prfx_
Then we install WordPress using the follow command.
wp core install --url=example.com --title="WordPress Website Title" --admin_user=admin_user --admin_password=admin_password --admin_email="email@example.com"
Backup WordPress database
Before doing anything else you should always have a backup of your database. You can use the
db command to quickly export a .sql file to the root of your website.
wp db export
Update WordPress with WP-CLI
Before updating WordPress you might want to check the version of your current WordPress installation. To do that you can use the
core command and it will return the current version.
wp core version
To then update your WordPress install all you have to do is run the following command below. You can start to see how fast using WP-CLI really is! You could backup your database, check the current version, and update WordPress, literally in a matter of seconds with only three commands!
wp core update
Update WordPress site URLs
If you are installing WordPress or moving it around from local to production you might need to update the WordPress and site address URLs. To do this you can use the wp option command.
wp option update home https://newaddress.com wp option update siteurl https://newaddress.com
Install WordPress plugins
Before installing new WordPress plugins you might want to check the status of what is already installed. To do this you can use the
wp plugin command. The following will display a current list of plugins installed and a legend letting you know if they are inactive, active, or if there is an update available.
wp plugin status
You can then install any plugin from the WordPress repository. That's right, you can even install KeyCDN's free Cache Enabler plugin with WP-CLI. The name is simply the end of the slug in the repository.
You can then install it by running the following command.
wp plugin install cache-enabler wp plugin activate cache-enabler
The same can be done to uninstall the plugin.
wp plugin uninstall cache-enabler
Update plugins with WP-CLI
Updating plugins is a very similar process. To do this we gain use the
wp plugin command.
wp plugin update cache-enabler
Install WordPress themes
Installing WordPress themes is almost identical to the plugins process. Perhaps you want to get an older WordPress theme back for testing. Remember, this is pulling from the repository. To do this we use the
wp theme command.
wp theme install twentyten wp theme activate twentyten
Update WordPress themes
Similarly you can also update WordPress themes using the
wp theme command. First you will probably want to grab a current list of your themes. It will show the version next to them.
wp theme list
To update a theme run the following command.
wp theme update twentyten
One of the biggest benefits of WP-CLI is of course automating some of those repetitive tasks, especially for those working with WordPress multisite. You can use bash scripts or do it in a single command like we used below to install our CDN Enabler plugin, Cache Enabler plugin, and Optimus Image Optimizer plugin all at the same time.
for site in $(wp site list --field=url); **do wp plugin install** cdn-enabler --url=$site --activate **wp plugin install** cache-enabler --url=$site --activate **wp plugin install** optimus --url=$site --activate; done
For launching new WordPress sites this opens up a whole new world of automation. Just imagine running only a few commands and your 20+ plugin WordPress site is up and going and backed up while you take a coffee break.
- Official WP-CLI website
- WP-CLI Wiki
- WP-CLI on Stack Overflow
- Alternative Installation Methods for WP-CLI
As you can probably tell you can get as advanced and intricate as you want with WP-CLI. With the ability to also create your own commands, the sky is pretty much the limit on what you can automate. If you haven't started using WP-CLI yet, give it a try, it might just become your new best friend as you unleash the power of the command line with WordPress.