10 Ways WP-CLI Can Speed Up WordPress Development
WP-CLI is a useful time-saving tool for WordPress developers and advanced WordPress users. However, if you’ve never used this command line package before, it can be a little intimidating to get started.
Most of us learn best by doing, so that’s what we’re going to do with this tutorial. That way, you can see WP-CLI in action and get a feel for what it’s like to use this powerful tool.
In this post, we’ll walk through 10 of the most useful things you can do with WP-CLI. Follow along, and by the end of the post, you’ll have a strong working knowledge of how to use WP-CLI to get things done quickly.
Getting Started with WP-CLI
The first thing you’ll need to do is find a place to use WP-CLI. Many hosts offer WP-CLI preinstalled on their servers — especially if they target the WordPress ecosystem. That means that the easiest way to get started with WP-CLI is usually to SSH to your web server and just start typing commands. (Check out our beginner’s guide to SSH if you need help).
If you find that you do you need to install WP-CLI, it can be installed on any UNIX-like environment. So it runs fine on all Linux web servers and can even be user-installed on many cheap shared hosting accounts with minimal fuss. It can also be installed on OS X and Windows, although some features may not work on Windows and you can expect it to be a little harder to get WP-CLI up and running on Windows.
Once you have WP-CLI up and running in the environment where you want to use it – the environment where your WordPress install will live – you’re ready to start typing commands.
The first thing you can do with WP-CLI is set up a new WordPress installation. Let’s do that now.
1. Install WordPress
You use the
wp core command to download, install, and update a WordPress installation. To use it, you need to first get into the directory where WordPress will be installed.
I created a new directory called /wp-cli/ and set it up to resolve as a subdomain of my personal website: http://wp-cli.jonpenland.com. So, to install WordPress at this location, I first moved to the appropriate directory with the command
cd ~/public-html/wp-cli and then used the following WP-CLI command to download WordPress:
Next, you’ll need to create a database. Your host may or may not give you a way to do this over SSH. Either create a database over SSH with the
create database command or log into your hosting account and create a new database using another host-provided tool like a MySQL database creation wizard or phpMyAdmin.
Once your database is ready, head back to the command line. You’re ready to set up wp-config.php with this command:
This command will create a configuration file that includes the details you provide. Use the database name, user, and password you created previously, your database host (probably localhost), and a database table prefix. The default prefix is wp_, but you’ll probably want to make it something unique like xyz_. Then just run the command.
The last step is to run the WordPress installation script, which you can do with this command:
Fine tune that command to fit your website, run it, and you’ll be greeted with the words: WordPress installed successfully. Now, open up your browser and point it at your website URL and you should be greeted by the default WordPress theme.
There’s a lot more you can do with the
wp core command, including:
wp core version: check the current version of WordPress.
wp core multisite-install: run this command instead of
wp core installto set up a multisite network rather than a standard WordPress installation.
wp core update: update the WordPress core.
wp core update-db: update the WordPress database.
We’ve spent a lot of time on installation. Thankfully, if you’ve made it this far, you’ve completed the hardest step we’re going to tackle in this tutorial. Let’s move on.
2. Install Themes
You use the
wp theme install command to add themes to WordPress. However, before doing that, you should check to see which themes are already available:
This will produce a table showing the list of installed themes, their status, version, and whether or not an update is available.
With WP-CLI you can install themes from the WordPress.org repository, themes stored in a zip file on the web server, and themes stored remotely in zip format (such as themes hosted at GitHub). For example:
Run the command successfully, and you’ll see something like this:
3. Install Plugins
Next, let’s install a plugin or two. You use the
wp plugin install command to install plugins. Just as with themes, we can check the list of currently installed plugins with the following command:
The results are formatted to look a lot like what was displayed when we ran the similar command for themes:
Let’s install two plugins.
Just as with themes, we can install plugins from WordPress.org, plugins saved to the server or plugins hosted in a remote repository. Let’s install the free versions of the Smush and Custom Sidebars plugins from WordPress.org
Manage unlimited WP sites for free
To install those plugins, we need to know the slug from the URL where they can be found in the official plugin directory. However, if we don’t want to look up those URLs, we can also search for them, like this:
That command will return a list of plugins that match the term “smush”:
Now we can see that the slug we need to use to install the plugin is wp-smushit. Next, let’s find Custom Sidebars.
It looks like custom-sidebars is the slug we’re looking for. With those two slugs in hand, we can now install the plugins with this command:
In a matter of seconds, both are installed.
We’ve run through the basics of installing WordPress, themes, and plugins. Next, let’s look at some site maintenance tasks you can take care of with WP-CLI
4. Check for Theme Updates and Apply Them
Updating themes with WP-CLI is done by adding the
update subcommand to the
wp theme command.
5. Check for Plugin Updates and Apply Them
A similar set of commands can be used to update plugins with WP-CLI:
6. Interact with a Plugin
There are several plugins that can be controlled with WP-CLI commands. For example, with Jetpack CLI you can interact with Jetpack modules, check current option settings, edit the Jetpack Protect whitelist, reset Jetpack, and more.
The commands that can be used with supported plugins vary from one plugin to the next. To learn more, consult the list of plugins that extend WP-CLI and then research the commands supported for any plugins you use.
Troubleshooting and Migration
WP-CLI may be more useful for troubleshooting and migration than any other task. That is because WP-CLI can manipulate a WordPress installation even when you can’t log into it. So the next time you get locked out of WordPress or find yourself staring at the white screen of death, fire up SSH and dive into WP-CLI before resorting to more drastic measures.
7. Deactivate or Activate a Plugin
Quite often, WordPress issues crop up when a plugin upgrade goes wrong or when you install a plugin that isn’t compatible with the active theme or another plugin. When that happens, try some of these commands to sort out the problem.
8. Activate or Deactivate a Theme
If it isn’t a plugin causing your problems, then it’s probably your theme. Here are the commands you can use to change your active theme and take back control of your WordPress website.
9. Rollback WordPress to a Previous Version
WordPress 4.7 includes some great new features like a revamped and improved Customizer, PDF thumbnail previews, and more. However, some users have run into trouble when upgrading to WordPress 4.7. If this happens to you, and you want to rollback to 4.6.1, you can do so with this command:
This will force WordPress back to the previous version and also delete any files that were new in WordPress 4.7.
It bears mentioning that you should always take a backup of your site before updating WordPress or forcing WordPress back to a previous version.
10. Search and Replace
WP-CLI’s search and replace command can be tremendously useful when migrating a WordPress website to a new domain or when implementing https.
Keep in mind, that you should always backup your database before running a search and replace command — and that goes for all search and replace tools, not just WP-CLI. Search and replace operations cannot be rolled back, so if you get one wrong, fixing the damage can be incredibly difficult and time-consuming, or even impossible.
--dry-run option in WP-CLI can help you avoid making costly mistakes. It’s always a good idea to use this option, which will return the number of replacements and list the tables where those replacements will take place without actually changing anything. That way, you can make sure the results make sense and are being applied where you expect to see them before running the command.
By default, the search and replace functionality in WP-CLI only replaces instances in the core WordPress database tables. However, you can search and replace specific tables, or all tables.
WP-CLI is a powerful tool and well worth taking the time to get comfortable with. If you’ve made it through this tutorial and created and manipulated a test WordPress installation along the way, then you’re probably beginning to see how much time WP-CLI has the potential to save you.
Like all powerful development tools, WP-CLI takes a little while to get the hang of, but once you work it into your workflow, you’ll wonder how you ever got anything done without it.Tags: