In a previous tutorial, we showed you how to install WordPress with WP-CLI. Now, let's go further in managing a WordPress installation.

This tutorial series on WP-CLI is intended to show you how effective this tool can be for you as a developer or as an advanced WordPress user.

In this tutorial, we will therefore cover:

How to install WordPress themes

You use the "wp theme install" command to add Themes on WordPress. However, before doing this, you should check and see what Themes are already available:

wp theme list

This will produce a table that will display the list of Themes installed, their status, version, and whether or not an update is available.

List of installed themes

With WP-CLI you can install themes from the directory of WordPress themes.org, themes stored in a zip file on the web server, and themes stored remotely in zip format (such as themes hosted on GitHub). For instance:

# Install the Tortuga theme from WordPress.org (https://wordpress.org/themes/tortuga/) wp install theme tortuga # Install the Store theme locally wp theme install ../tortuga.zip # Install the Stored theme remotely wp theme install https://github.com/jpen365/five-hacks-for-twenty-seventeen/archive/1.0.zip

Run the command successfully, and you will see something like this:

Wordpress theme installation

How to install WordPress plugins

Next, we'll install a plugin or two. You will use the "wp plugin install" command to install plugins. Just like with themes, we can check the list of installed plugins with the following command:

wp plugin list

The results are formatted and will be quite similar themes:

List of wp cli plugins

Install two plugins.

Just like with themes, we can install plugins from WordPress.org, plugins saved on the server, or plugins hosted in a remote repository. Let's install the free versions of Smush and Custom Sidebar available on WordPress.org.

To install these plugins, we need to know the URL slug where they can be found in the official plugin directory. However, if we don't want to see these URLs, we can also search for these plugins as well:

wp plugin search smush

This command returns a list of plugins that match the term "smush":

List of plugins search wp cli

Now we can see the slug of the plugin we need to use. In this case, we are going to install the "wp-smush" plugin. Next, we'll search for “Custom Sidebars” the same way.

Wp cli plugin search

It sounds like "custom-sidebars" which is the slug we're looking for. With these two slugs in hand, we can now install the plugins with this command:

wp plugin wp-install custom-smushit sidebars

In a few seconds, both plugins are installed.

Installation of wordpress wp cli plugins

Some maintenance operations with WP-CLI

We ran through the basics of installing WordPress, themes, and plugins. Next, let's explore some tasks on maintaining the site with WP-CLI now.

How to check for theme updates and apply them

Updating themes with WP-CLI is done by adding the " Update »For order« Wp theme ».

# Check updates for all themes wp theme list # Check for updates available wp theme update --all --dry-run # Update all themes wp theme update --all # Update a theme wp theme update twentyseventeen

How to check plugin updates and apply them

A similar set of commands can be used to update plugins with WP-CLI:

# Check updates wp plugin list # View plugins that WP-CLI can put wp plugin update --all --dry-run # Update all plugins wp plugin update --all # Update a single plugin wp plugin update smushit

How to troubleshoot and migrate

WP-CLI can be more useful for troubleshooting and migrating from one blog to another. This is because WP-CLI can manipulate an installation of WordPress, even though you cannot log into it. So the next time you're stuck or facing the dashboard, using SSH with WP-CLI might help you fix it.

Enable or disable plugins

Very often, problems arise when a plugin update goes wrong or when you install a plugin that is not compatible with the active theme or another plugin. When this happens, try some of these commands to fix the problem.

# Disable plugin wp plugin deactivate hello-dolly # disable and uninstall a plugin wp plugin deactivate hello-dolly --uninstall # uninstall a plugin olden disabled wp plugin uninstall hello-dolly # Disable all plugins wp plugin deactivate --all # force the versiond of a wp plugin plugin install hello-dolly --version = 1.5 --force

Enable or disable a theme

If it's not a plugin that's causing your problems, then it's probably your theme. Here are the commands you can use to change your active theme in order to take control of your WordPress site.

# Activate a theme wp theme activate twentyseventeen # Delete a deactivated theme wp theme delete twentysixteen # Disable a theme we multisite network wp theme twentysixteen disable # Enable a theme multisite network we enable wp theme twentyseventeen

Find and replace text

WP-CLI's find-and-replace command can be extremely useful when migrating a WordPress site to a new domain or when implementing https.

# Will show the number of occurrences wp search-replace "http://olddomain.com" "http: // newdomain" --dry-run # Search and replace a domain wp search-replace "http://olddomain.com "" http://newdomain.com "# Update wp search-replace links" http://example.com "" https://example.com "

Keep in mind, that you should always back up your database. data before running find and replace commands, and that goes for all find and replace tools, not just with WP-CLI. Search and replace are operations that cannot be undone, so if done incorrectly, fixing problems can be incredibly difficult and time-consuming, if not impossible.

WP-CLI's “–dry-run” option can help you avoid costly mistakes. It's always a good idea to use this option, which will return the number of replacements and the list of tables where those replacements will take place without changing anything. This way you can make sure that the results make sense and are applied where you expect to see them before running the command.

# Search and replace a string in the wp_post table and table customizes wp search and replace "old-string" "new channel" wp_custom_table wp_posts # search and replace throughout the wp tables search and replace "old-string" "new channel" --all-tables

By default, the " search and replace of WP-CLI only replaces instances in the base tables of data WordPress by default. However, you can search and replace on specific tables, or on all tables.

That's all for this tutorial, I hope it will allow you to have more control with the WP-CLI tool. If you have any questions, do not hesitate to contact us.