Developing with WordPress – part 4: WP-CLI basics
WP-CLI. This has to be my favorite tool out there to work with WordPress installations. In fact, I like it so much I even dedicated a talk about it on WordCamp Helsinki 2017. There is much you can do with WP-CLI but today we’ll focus on the basics.
What is WP-CLI
WP-CLI stands for WordPress Command Line Interface. And as the name suggests, you will use it via the command line. WP-CLI comes default installed with VVV so you don’t have to worry about that. You can use WP-CLI to execute commands from the command line that you normally would do in wp-admin. For example: you can install or update your plugins, your themes and even WordPress core. Or you can manage the comments on your site, execute some database actions and much more.
As you can do all those commands also in wp-admin you could ask yourself: why should I use WP-CLI? And the answer is productivity. Once you get the hang of it, you can use WP-CLI to execute tasks much faster than you would in wp-admin. And WP-CLI is great for automating tasks as well. As this series’ focus is on improving your WordPress workflow, WP-CLI is a tool that can do a lot in this matter.
Okay, that’s enough theory. Let’s get started with how to use WP-CLI. Before continuing make sure you have a working installation of VVV with at least one WordPress site running.
First you have to SSH into VVV and go to the root folder of your site:
$ vagrant ssh
$ cd /srv/www/wordpress-develop
Now you can start using the WP-CLI commands. You’ll need to type
wp followed by the action you want to perform. For example, listing all the plugins of your site you can do with the
wp plugin list command:
$ wp plugin list +---------+----------+-----------+---------+ | name | status | update | version | +---------+----------+-----------+---------+ | akismet | inactive | available | 4.0.1 | | hello | inactive | none | 1.6 | +---------+----------+-----------+---------+
As you can see, this shows you all the info on all your plugins: whether they are active or not, whether there are updates available and which version is currently installed.
Let’s activate the akismet plugin:
$ wp plugin activate akismet Plugin 'akismet' activated. Success: Activated 1 of 1 plugins.
Let’s update the plugin as well:
$ wp plugin update akismet Enabling Maintenance mode... Downloading update from https://downloads.wordpress.org/plugin/akismet.4.0.3.zip... Unpacking the update... Installing the latest version... Removing the old version of the plugin... Plugin updated successfully. Disabling Maintenance mode... +---------+-------------+-------------+---------+ | name | old_version | new_version | status | +---------+-------------+-------------+---------+ | akismet | 4.0.1 | 4.0.3 | Updated | +---------+-------------+-------------+---------+ Success: Updated 1 of 1 plugins.
Not only does it update the plugin, it also puts your site in and out of maintenance mode while doing so. Nice!
It’s also possible to install a new plugin straight from the command line:
$ wp plugin install polylang Installing Polylang (2.3.4) Downloading installation package from https://downloads.wordpress.org/plugin/polylang.2.3.4.zip... Unpacking the package... Installing the plugin... Plugin installed successfully. Success: Installed 1 of 1 plugins.
Note that this only installs the plugin. You’ll still need to activate it. As you usually want to activate a plugin straight when your are installing it, you can install and active the plugin with one command:
$ wp plugin install woocommerce --activate Installing WooCommerce (3.3.4) Downloading installation package from https://downloads.wordpress.org/plugin/woocommerce.3.3.4.zip... Unpacking the package... Installing the plugin... Plugin installed successfully. Activating 'woocommerce'... Plugin 'woocommerce' activated. Success: Installed 1 of 1 plugins.
So far we’ve only used commands that modify the status of one plugin. You can also tell WP-CLI to do something with all plugins at once, by using the
$ wp plugin deactivate --all Plugin 'akismet' deactivated. Plugin 'hello' deactivated. Plugin 'polylang' deactivated. Plugin 'woocommerce' deactivated. Success: Deactivated 4 of 4 plugins.
This was just an introduction to all you can do with plugins. You can do the same for your themes, just replace
theme in all examples above.
Managing WordPress core
As I mentioned above, you can manage WordPress core with WP-CLI as well.
$ wp core version 4.9.1
Shows you the current version of WordPress core. As this is not the latest version of WordPress, let’s perform an update:
$ wp core update Updating to version 4.9.4 (en_US)... Downloading update from https://downloads.wordpress.org/release/wordpress-4.9.4-partial-1.zip... Unpacking the update... Cleaning up files... No files found that need cleaning up. Success: WordPress updated successfully.
As you can see, updating WordPress core can be done with one command only. You can even use WP-CLI to completely install a new WordPress site from scratch, without opening your browser!
The last topic I want to discuss today is managing your database with WP-CLI. You often will need to export or import a database, and WP-CLI makes that easy for you:
$ wp db export db_latest.sql Success: Exported to 'db_latest.sql'.
You can change db_latest.sql to whatever filename you prefer. If you leave it out, then WP-CLI will automatically generate a filename for you.
Importing goes just as easy:
$ wp db import db_latest.sql Success: Imported from 'db_latest.sql'.
Getting a database from your production site into your local site has never been easier! But wait… WordPress usually saves the URL of your site in the database, right? So all the links in the database will then point to the production site instead of your local. Luckily, WP-CLI can help you out here as well:
$ wp search-replace https://www.thisismysite.com http://local.wordpress.test Success: Made 6 replacements
As a matter of fact, you can even export a database, run the search-replace over the results, and save it in a file all in one command:
$ wp search-replace http://local.wordpress.test https://www.productionsite.com --export=db_latest.sql Success: Made 6 replacements and exported to db_latest.sql
This way the source database stays untouched, and the generated SQL file already contains the correct replacements of all your URLs. Try doing that so quickly in wp-admin.
I hope this has been a good introduction to WP-CLI. We will come back on this in a later post with more advanced uses of this amazing tool, but if you can’t wait then I strongly suggest you to check out the official website. There are plenty of examples to find there.
We will discuss another topic next week. Stay tuned!