New Dev Tool: WP-CLI

One tool I’ve been using for several months is WP-CLI. This tool is an absolute must if you are doing any server work. WP-CLI from their website, is a command-line tool for managing WordPress installations. It gives you the abilities like update plugins, themes, even WordPress. You can even do database tasks.

The database tasks are probably the most important for me. This is pretty sweet because you could create a system that would provide backups of your database very easily. Let’s take a look at what it would look like.

First off, if you wanted to do a backup of a database it would look something like this with MySQL commands:

mysqldump -u<username> -p<password> database > /file/location/filename.sql

This should look familiar to most people. But we’ll break it down right now. The first, mysqldump. This is the command that tells mysql you want a dump of a databse. The options -u and -p are pretty simple, you want to pass the username and passwords. Then we add the database we’d like to get the backup from, and finally we add in the file location and filename of the sql file we’d like to create.

With WP-CLI though, we could do something else. Move into the directory where WordPress is installed.

wp db export filename.sql

This looks much easier. We’ll go ahead and break this one down too. The first, wp, is the command for WP-CLI. The db references the database, export is the command that you want to do. Finally we have the file name at the end. Pretty easy right!

A really cool thing about WP-CLI is that it allows you to write packages for it. Basically adding functionality for WP-CLI. You can see a growing list here.

The next time you are doing development work or some server work, make sure to take a look at WP-CLI.