10 cool things you can do with WP-CLI

WP-CLI, short for WordPress Command Line Interface, is a powerful and versatile tool designed to facilitate the management of WordPress websites through the command line. It provides a command-line interface for developers, administrators, and power users to interact with various aspects of their WordPress installations directly from the terminal.

With WP-CLI, users can perform a wide range of tasks very efficiently. This command-line interface streamlines and automates many routine tasks, offering an efficient alternative to traditional web-based administrative interfaces.

Enough with the introduction, here are some commands you can use with WP-CLI to make your WordPress website management easier.

1. Search & Replace

If you need to search and replace something on your WordPress site, this is probably one of the easiest ways.

wp search-replace

For example, it can be used to search and replace a URL after moving a website.

wp search-replace <old-url> <new-url>

More details on how to use this command.

2. Importing or exporting your database

With WP CLI, you can import…

wp db import

… or export a database.

wp db export

This is much faster than going through PHPMyAdmin or other database tools for this, very useful to do a quick backup before making some changes.

More details on how to use the db import and db export commands. There are also many other commands available for database interaction.

3. Deactivate a plugin to bring back a site

Sometimes, plugin updates don’t go as planned and you end up with a broken site. The following command allows you to deactivate the faulty plugin to get your website back on track and be able to work on the issue.

wp --skip-themes --skip-plugins plugin deactivate faulty-plugin-slug

More details on how to use this command.

4. Get info about your WP CLI install

To ensure that your WP CLI install is working properly, you can run this command and check the details.

wp --info

5. Clean post revisions

For larger sites, post revisions can take up quite a bit of space. From time to time, it’s a good idea to clean up revisions, as they are often unnecessary. To use this command, you will need to install the wp-revisions-cli package.

wp revisions clean --post_id=ID --before-date=DATE

6. Repair & optimize your database

Another maintenance routine you should consider for your WordPress site is to repair and optimize your database on a regular basis.

To repair your database with WP-CLI:

wp db repair

To optimize your database with WP-CLI:

wp db optimize

7. Activate maintenance mode

When you are updating a website, especially a larger one, it’s a good idea to put it on maintenance mode before starting upgrades, in order to not have some processes disturbed by the update.

To activate maintenance mode with WP-CLI, you can just use this:

wp maintenance-mode activate

To deactivate it, just use this command:

wp maintenance-mode deactivate

8. Update password with WP-CLI

If you lose your password as an administrator in WordPress and you have no access to the email the account was created with (for example: when you take over a new website), you can go to PHPMyAdmin to modify the password directly in the database, or you can just use WP-CLI and do it much quicker.

wp user update admin@website.com --user_pass=new-password

9. Create a child theme

As a developper, you know that to create a child theme in WordPress is the best way to customize an existing theme and survive updates. To do it with WP-CLI, use a function like this:

wp scaffold child-theme slug-of-child-theme --parent_theme=slug-of-parent-theme --theme_name=theme-title

10. Generate filler posts

When you install a new instance of WordPress, it can be useful to have filler content to properly test it. For that, you can just use the generate command this way and create as many posts as you want (the following example generates fifteen posts).

wp post generate --count=15

See all the options for this command on this page.

Tags: