There’s a lot more you can do with the
wp core command, including:
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.
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:
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:
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
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
Updating themes with WP-CLI is done by adding the
update subcommand to the
wp theme command.
A similar set of commands can be used to update plugins with WP-CLI:
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.
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.
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.
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.
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.
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.