Advertisement
  1. Code
  2. Hosting

Using wp-cli for Fun and Profit

Scroll to top
Read Time: 5 min

See how you can perform common WordPress tasks faster or even automate them by using the power of bash.


What Is wp-cli?

wp-cli is a tool for controlling WordPress through a console window.

When the WordPress admin interface is so beautiful and easy to use, the natural question is: why would you ever use a command-line interface?

There are two main reasons:

  • The keyboard is faster than the mouse - For power users, typing a command can be orders of magnitude faster than pressing a button in a web browser.
  • Scripting - You can put several commands in a text file and have it executed automatically.

In this tutorial I'm going to give you a taste of what wp-cli is good for.


Installing wp-cli

Before using it, you'll need a few things:

  1. UNIX-like shell - If you're on a Mac or on Linux, you can run wp-cli on your local machine. If you're on Windows, you can log into your Linux server via Putty and install wp-cli there.

  2. PHP 5.3+ and the php-cli utility - You can see if you have these by running the following command:

    1
    php -v
    
  3. Git - This is what we'll use to install and update wp-cli.

The wp-cli project is still in it's infancy, so the best way to stay on top of recent versions is by cloning it from github:

1
2
git clone --recurse-submodules git://github.com/andreascreten/wp-cli.git ~/git/wp-cli
3
cd ~/git/wp-cli
4
sudo utils/build-dev

~/git/wp-cli is the directory where wp-cli will be installed. Feel free to change it to whatever you want.

That's it. Now the wp command should be available:

1
2
cd /var/www/public_html/wordpress
3
wp
`wp` output`wp` output`wp` output

On Multisite

If you want to use wp-cli on a multisite install, you'll have to decide which blog you want to operate on:

1
2
wp --blog=myblog.mynetwork.com

To avoid having to pass the --blog parameter for each command, you can store it in a specially-named file:

1
2
echo 'myblog.mynetwork.com' > wp-cli-blog

wp-cli will read that file if there's no --blog parameter.


Handling Core

First, let's see what version of WordPress we're dealing with:

1
2
wp core version --extra
`wp core version` output`wp core version` output`wp core version` output

To perform an update, you just need to write:

1
2
wp core update
`wp core update` output`wp core update` output`wp core update` output

Handling Plugins

Let's see what plugins we have installed:

1
2
wp plugin status
`wp plugin status` output`wp plugin status` output`wp plugin status` output

Huh, it seems there's an update available for Akismet. Let's install it:

1
2
wp plugin update akismet
`wp plugin update` output`wp plugin update` output`wp plugin update` output

Now let's install and activate a plugin from wordpress.org:

1
2
wp plugin install google-sitemap-generator --activate
`wp plugin install` output`wp plugin install` output`wp plugin install` output

Also, you can install the development version of a plugin:

1
2
wp plugin install google-sitemap-generator --activate --dev

Oh, and look, there's a new command available now:

1
2
wp google-sitemap
`wp sitemap help` output`wp sitemap help` output`wp sitemap help` output

We can quickly switch a plugin from active to inactive and vice-versa:

1
2
wp plugin toggle google-sitemap-generator
`wp plugin toggle` output`wp plugin toggle` output`wp plugin toggle` output

This is a good way to debug activation hooks.

Similarly, you can run a plugin's uninstall procedure without deleting the plugin files:

1
2
wp plugin uninstall google-sitemap-generator

And, of course, you can delete the plugin as well:

1
2
wp plugin delete google-sitemap-generator

Handling Themes

We have a few commands for working with themes too:

1
2
wp theme status
`wp theme status` output`wp theme status` output`wp theme status` output

Unlike plugins, you can only have a single theme running at a time, so activating a theme will automatically "deactivate" the previous one:

1
2
wp theme activate twentyten

And here's a little trick to go into the directory of a particular theme:

1
2
cd $(wp theme path twentyeleven)

Generating Data

If you're writing a theme and you want to style the pagination, you're going to need a lot of posts. Here's the quickest way to get them:

1
2
wp generate posts --count=1000
`wp generate posts` output`wp generate posts` output`wp generate posts` output

If you want to style a list of users, you can generate some of them too:

1
2
wp generate users --role=author
`wp generate users` output`wp generate users` output`wp generate users` output

You can also create individual users:

1
2
wp user create stan stan@company.com
`wp user create` output`wp user create` output`wp user create` output

Creating Export Files

You might want to periodically export your content to a WXR file.

1
2
wp export --path=./ --user=admin
`wp export` output`wp export` output`wp export` output

You can pass additional parameters to limit what content is exported, such as --category, --start_date etc.


Changing Options on the Fly

There are straightforward commands for CRUD operations on options:

1
2
wp option get permalink_structure
3
wp option add foo bar
4
wp option delete foo

You don't want to do this on a regular basis, as most options are constrained to certain values. But it can come in handy in scripts.


Database Operations

If you want to make a backup of the database, just write:

1
2
wp db dump
`wp db dump` output`wp db dump` output`wp db dump` output

Or perhaps you need to do a quick query to find when the last post was published:

1
2
wp db query "SELECT MAX(post_date) from wp_posts WHERE post_type = 'post' AND post_status = 'publish'"

Opening an interactive MySQL session to do some diagnostics is just as easy:

1
2
wp db cli

Running Arbitrary Code

Sometimes, the only way to tell wp-cli what you want is by describing it in PHP code:

1
2
wp eval-file do-my-laundry.php

With the above command, wp-cli will first load WordPress and then load and execute your PHP file.

This is useful in deploy scripts or for other complex actions that can't be achieved using built-in commands.

You can also pass PHP code inline:

1
2
wp eval 'echo WP_CONTENT_DIR;'

Creating Your Own Commands

Believe it or not, wp-cli is written mostly in PHP. Each command is a class, with each method representing a subcommand.

The neat thing is that you can make your own class, put it in a plugin and wp-cli will automatically recognize it as one of it's own. A detailed tutorial for creating commands is available in the project wiki.


Conclusion

I hope I've convinced you to at least give wp-cli a try. If you've found a bug or if you have a feature request, consider opening an issue.

Have an interesting use case for wp-cli? Please share it in the comments below.

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.