WP-CLI: A swiss army knife for WordPress developers

WordPress LogoWP-CLI is a command line WordPress administration tool. You can use it to perform various tasks on your WP installation while logged in via SSH.

Getting started

Installing WP-CLI is a case of downloading the latest release from their github repo and then placing it somewhere in your PATH. Usually on linux distros ~/bin is included in the PATH variable so put it there, rename it to just ‘wp’ and chmod +x it so that it becomes executable. Do an echo $PATH and if ~/bin isn’t there add it:

export PATH="$PATH:$HOME/bin"

WP-CLI has excellent documentation built in. Just type:

wp help

To see all the commands you can give it. To get help on a specific command type:

wp help <command name>

You can do all sorts of things with WP-CLI. You can post new content, edit existing content, manage themes and plugins, manage users, run queries on the database and install and upgrade WP core as well.

WP-CLI

To manage a WP site with WP-CLI you have to first change to the site’s document root directory. Then when you run WP-CLI it’ll automatically know which site you mean.

Core Management

WP core management is handled with the core commands. Installation is done with these commands:

wp core download #download core from wordpress.org and place the files in the current directory
wp core config --prompt #create the wp-config file and prompt for database details.
wp core install --prompt #run the installation scripts prompting for site details

It seems like a lot of steps but there’s a logical progression to it and it’s all scriptable.

Updating is easier:

wp core update #download and replace files
wp core update-db #update the database

Database administration

One of the more interesting things is running db queries:

wp db query "show tables"

Or creating a database backup:

wp db export backup.sql

Maybe you’d like to import an existing backup:

wp db import backup.sql

Being able to do all this via the command line without having to provide the mysql username and password each and every time is very convenient.

Another interesting database related command is the search-replace one. It allows you to replace strings in the database. This is a great tool to use when migrating a site from dev to production. You can easily change the URL in all database tables with this command. It will correctly handle serialized values as well:

wp search-replace http://localhost/ http://example.com/ --precise

Rewrite rules

WordPress rewrite rules are a bit of a headache. They are hard to get right so WP CLI has a command related to that as well. Try listing them:

wp rewrite list | less

Flush the rules if you don’t see your rewrites in the list:

wp rewrite flush

WP-CLI Console

This is an interactive console that evaluates PHP from within your WP site environment! Sometimes you just want to make a small one time change without having to create a whole plugin for it. This is what the shell option allows you to do:

wp shell

Conclusion

WP-CLI is a great command line tool that makes developing and administering WP sites so much easier and I highly recommend you try it out!

Leave a Reply

Your email address will not be published. Required fields are marked *