Managing Wordpress from the Command Line with WP-CLI

Phuse
Phuse
Published in
4 min readNov 27, 2014
WP-CLI

Recently I had the pleasure of attending Toronto’s WordCamp conference, a weekend of WordPress-related presentations geared towards all levels of users. By far the best and most informative presentation was Shawn Hooper’s presentation, “Saving Time by Managing WordPress from the Command Line.”

This session was a game changer — I don’t think the workflow of any of those in attendance will be the same after learning these amazing time saving tips.

I came across the WordPress Command Line (WP-CLI) search and replace command a few months back, but this session opened my eyes to more of its amazing features that many WordPress developers, like myself, have been missing out on.

So what exactly is the WP-CLI?

Simply put, the WP-CLI is a set of tools for managing WordPress installations from the command line. You can view the requirements, steps for installation and basic usage on the WP-CLI official site. In short, you install the WP-CLI on your computer but run it anywhere within the structure of the WordPress folder of your site (with the exception of the installation of a new WordPress instance).

You need to have SSH access to your server in order to use these tools, which means many shared hosts don’t support use of the WP-CLI, so be sure to do your research first!

Now let’s get to the good stuff! Below is a recap of some of the amazing commands I picked up in Hooper’s session at Toronto’s WordCamp conference.

The core Command

With this command, you can check the version of an existing install and run an update or even convert an existing site to a multisite. Additionally, you can install WordPress without ever opening a browser with the commands below.

wp core download
wp core config
wp db create
wp core install

Yes, WordPress’s famous “5 Minute Install” has now become a 5 second install.

The plugin Command

The wp plugin command allows you to find, install, activate, deactivate and update plugins among other things.

Ever run into a situation where you update or activate a plugin thatthrows an error and brings your whole site down, giving you the dreaded white screen of death and a small panic attack? No need to try to fix things via FTP. With this tool, you just simply need to run wp plugin deactivate — all to turn off all installed plugins allowing you to access your site once again!

The db Command

This command allows you to interact with your MySQL database. That’s right, you can export or import a backup, and even log in to your database without having to go through MySQL (i.e. without having to find that password that’s too complicated to remember off-hand).

The search-replace Command

Moving from your staging environment to production? Need to update all of the site URL references in the database? Easy! After importing a copy of your staging database into your production environment, run this command:

wp search-replace '<old URL>' '<new URL>'

For example:

wp search-replace 'http://staging.mycompany.com/client' 'http://clienturl.com'

Add — dry-run to the end your command if you want to see a preview of what will be changed.

The post Command

You can use this command to create sample content for any post type (including custom post types). You can specify the number of posts you need, which can be handy for instances where you need a certain number of posts to test out your pagination. For example:

wp post generate --count=10 --post-content /file/testfile.txt

The above example would generate 10 posts with sample content from the specified document.

The media Command

I’m a big fan of using the Regenerate Thumbnails plugin after I’ve added a new or edited an existing custom image size. However, the wp media regenerate command allows you to accomplish the same goal without having to add another plugin.

The scaffold Command

While the scaffold command itself offers a number of useful items, it was specifically the use of wp scaffold post-type for generating PHP code for registering a custom post type that I found particularly useful. This can also be applied to generating custom taxonomies!

This is just a sample of what you can accomplish with WP-CLI. If you’re interested in learning more, check out some of the links below. There are a few videos yet to be uploaded, but we’ll add those to this post when they become available!

Do you use WP-CLI? Share your thoughts in the comments below!

--

--

Phuse
Phuse
Editor for

Phuse is a remote-based design and development agency headquartered in Toronto. We craft websites, interfaces and brands.