Best WP-CLI Commands to Help You Manage Your WordPress Website

10+ WP-CLI Tricks to Manage Your WordPress Site

WordPress is a fantastic project, but as with any piece of software, there may be times when you wish you had a backdoor into your code and database. You know what we’re talking about if you’ve ever attempted to reset your admin privileges using a faulty reset password link.

Fortunately, there is a workable option. With command-line access to your WordPress installation provided by The WP-CLI project, you can control the majority of your website with a few simple terminal commands.

For instance, you can easily update your password using the WP-CLI if your “reset password” link isn’t working. WP-CLI is useful in a variety of circumstances that are similar to each other. Additionally, we simply adore the hacker-like sensation that comes from using the command line.

In this article, we’ll describe and demonstrate how to complete a few of the generally tedious chores that WP-CLI almost turns into a delightful experience. Let’s get to work!

Table of Contents:

What is the WP-CLI Project

Since this application allows you command-line access to the WordPress administrative system, WP-CLI stands for WordPress Command-Line.

On August 8th, 2018, WP-CLI v2 was released. If you’re a FastComet customer, WP-CLI v2.0+ is already installed by default on all of our servers. To get started, just SSH into your server. You can be sure that all of our hosting plans include SSH access.

Typically, managing a WordPress website manually requires using the online user interface (the wp-admin). Consequently, fixing a malfunctioning site or overseeing a transitional time frequently entails sifting through files using an FTP client or attempting to write original PHP code. WP-CLI eliminates the need for additional plugins and streamlines the process of managing your website by providing a direct-access shortcut.

There is a slight learning curve if you have never used the command line before. WP-CLI, however, can greatly speed up how you deal with your website once you get started. With only a few clicks, you can modify complicated databases, plugins, and themes. You can also utilize the plain-text interface to control menus, choices, and comments if you like.

In the end, WP-CLI simplifies and accelerates a variety of challenging activities. Additionally, WP-CLI can be expanded. If you have specific requirements, you can write unique scripts that execute using WP-CLI according to your requirements.

Before continuing, it’s crucial to understand that WP-CLI is a server-installed utility rather than a plugin for your WordPress website. In other words, for consistent access, you must install WP-CLI on both your live hosting server and your local development environment.

Best WP-CLI Commands to Manage your WordPress Site

Using the command line, you can open the folder for the WordPress website you want to control after installing WP-CLI. You’ll need secure shell access if you’re working in real-time on your hosting server.

But first, we advise testing these commands in a development setting! Before utilizing this tool on a live server, you should be reasonably comfortable with it because there is no undo option. Here are four helpful ways to practice utilizing WP-CLI on your site to get you started.

Look Into the the wp-config.php File

When it comes to enabling you to configure a new site, WP-CLI has always been quite effective. For instance, you may create a new config file and include all the necessary information in it using the handy wp config create command. usually completed considerably more quickly than the conventional method, especially if you have a quick typing speed.

WP-CLI, however, goes a step further. The primary wp config command has two subcommands.

The first one is this:

wp config get

This lists constants and globals as defined in your current wp-config.php file.

WP Config Get WP-CLI

The other one is this:

wp config path

This command simply displays a path to the current wp-config.php file.

Another WP-CLI command is available if you wish to interact with your WordPress installation even more closely:

wp db size --tables
WordPress Database Size

Very helpful in assisting you in determining whether or not you require database improvement.

Run Core Updates

Running an update often entails login into your admin and visiting a few pages to update your site.

With WP-CLI, updates are easily run with one simple command:

wp core update

The ability to accept parameters to alter the behavior of the method makes it special. Consider the case where you recently updated WordPress to a version that broke a crucial plugin for your website. Although the creator of that plugin hasn’t had a chance to update it yet, your website must function properly. Although updating a site regularly is preferable, you can change the core by adding the version flag to your command and going back to a time when there was no conflict:

wp core update --version=6.02
WordPress Core Update

Update, Disable, and Delete Unwanted Plugins

Normally, to deactivate a plugin that has fully broken your site (or even just disabled your admin), you must get in via FTP and modify the name of the plugin’s folder. On the other side, deactivating plugins may be done with a simple WP-CLI command:

wp plugin deactivate plugin-name-example

If a plugin is giving you serious trouble and needs to be deleted, you can do so using the delete command instead:

wp plugin delete plugin-name-example

Additionally, you can avoid manual plugin updates by using a command that works similarly to the WordPress core update feature:

wp plugin update --all

Additionally, you can check the status of the plugins that are currently installed on your website using WP-CLI, which is probably something I should have stated at the outset. For that, employ:

wp plugin status

These instructions might not seem like they would save a lot of time when used alone. It’s crucial to keep in mind, nevertheless, that visiting the server directly is far quicker than waiting for a web browser to render all the scripts and styles necessary for a graphical user interface. You might be astonished by how quickly your additional time mounts up if you routinely have to do this kind of work.

Reset User Passwords

Consider a scenario where you forgot your WordPress admin password. You’ve attempted the link to reset your password, but for some reason, your mailbox isn’t receiving the reset email. Are you permanently unable to access your WordPress website? With WP-CLI, not so.

With WP-CLI, quick user management is possible, and with the line below, you may reset passwords.

wp user update [email protected] --user_pass=new-password-example
WP-CLI User Update

There’s no need to freak out or fret about difficult workarounds to regain access to your site with simple commands. The days of manually hashing your password and entering it into the database are long gone.

Additionally, utilizing this method does not compromise the security of your website because everything is done over a secure connection to your server.

Backup and Manipulate the Database

Direct database interaction is difficult to achieve with WordPress. When you wish to carry out ostensibly straightforward operations, like running an update without a plugin or looking up and changing a preset phrase, this might be irritating.

To make these tasks simpler, use WP-CLI. You can optimize and repair databases with straightforward commands like:

wp db optimize
wp db repair

If you are writing a custom query, you can quickly test it out by using the query command:

wp db query "SELECT * FROM wp_options"

However, it’s imperative that you understand how to establish backups before you become very trigger-happy with the database. The command to use in WP-CLI to create SQL backups is as follows:

wp db export

If something goes wrong, you can utilize the SQL file that is created as a backup of your site’s database. Even when used with backup plugins, it performs effectively.

The name that will be provided to the file is somewhat arbitrary. Use this to assign your own:

wp db export yourname.sql

Working with the database on your site is a breeze, thanks to WP-CLI.

Clean Post Revisions

The number of changes on some of our most regularly updated posts exceeds 200, which makes managing them extremely difficult.

The bar at the top appears when you access WordPress’ standard revision comparison page (see below screenshot). You can select the two revisions you want to compare through it. If a post has fewer than 20 revisions, this is OK. However, if there are more than 100, the bar is no longer clickable, making it difficult to quickly select the precise revision you want and necessitating pixel-perfect clicks. The bar is simply too thick.

To battle this in some way, you can set a limit on the number of revisions allowed per post in your wp-config.php file (eg. define('WP POST REVISIONS', 40);). However, this merely restricts future acts that you take. All current revisions that are higher than that threshold must be removed. There are several ways to accomplish this. One is done using a plugin. The second is through WP-CLI.

And the WP-CLI way is just much quicker. To get started, you need to install an additional WP-CLI package called wp-revisions-cli.

An easy command can be used to accomplish this:

wp package install trepmal/wp-revisions-cli

After that, you get to use a handful of cool new WP-CLI commands:

wp revisions clean

That eliminates all posts’ previous revisions. Only the most recent changes are kept by default, which is WP_POST_REVISIONS.

As an alternative, if you choose to be more involved in the revision process, you can, for example, manage the revisions for a particular post:

wp revisions list --post_id=ID

This command gives you a list of all the revisions for a given post.

Or:

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

which deletes edits to a specific post that was made on or before a certain date (YYYY-MM-DD).

Overall, a cool function, especially if you frequently edit your existing posts, which results in numerous revisions. I’m interested to see what future updates the wp-revisions-cli package will offer.

Create a Child Theme

Making child themes is not really challenging. To make sure that some minor details haven’t been altered, you must first check the official reference at Codex. Then, you must create a functions.php file, place CSS in a new directory, and so on and so forth.

You can use a a quicker method with WP-CLI. You simply do this:

wp scaffold child-theme NEW-CHILD-SLUG --parent_theme=SLUG --theme_name=TITLE

Just that. An operable, blank child theme built on a predetermined parent theme is what you receive as a consequence.

A few extra parameters are also available. For example, you could try this:

wp scaffold child-theme SLUG --parent_theme=SLUG --theme_name=TITLE --author=FULL-NAME --author_uri=URI --theme_uri=URI --activate --force
  • --force overwrites files that already exist.

Create Dummy Content for Testing

Dummy content is something you most likely need rather frequently when working on any kind of WordPress project.

  • It’s possible that you’re developing a new plugin that modifies posts after they have been processed.
  • Perhaps you want to check to make sure your pagination is flawless.
  • Perhaps you should add more items to your menus for testing.

In such situations, I used to typically press Ctrl+F and begin looking for that annoying XML dump of, for instance, WordPress material that I had lying around somewhere.

Anyway, as you’d imagine, WP-CLI has some options for that too:

wp post generate --count=6

This command creates 6 empty posts, but you don’t need to stop at just 6 – WP-CLI promises that 1000 is also a perfectly executable value.

Or, to make things more factual:

curl http://loripsum.net/api/4 | wp post generate --post_content --count=6

The command creates 6 posts, each having 4 paragraphs of dummy lorem ipsum text imported from loripsum.net.

Blank Your Site

One pretty cool WP-CLI command enables you to completely remove all of the content from your site, including posts, comments, terms, and meta while keeping the users and site setup untouched. This is it:

wp site empty

The amount of time this will save you will be immediately apparent if you frequently utilize the same development WordPress setup for your testing, experiments, etc.

This WP-CLI command is incredibly quick, even if you can get the same result by manually looking through your content or using phpMyAdmin.

You could also:

wp site empty --uploads

The command gets rid of everything in your “uploads” folder as well.

Reinstall WordPress Core

Using WP-CLI, you can also reinstall WordPress core. The WordPress core would be downloaded without the pre-installed themes and plugins using the following command:

wp core download --skip-content --force

Remove All Spam Comments

First of all, WP-CLI offers you a variety of options for managing comments. You can, for instance:

  • create new comments with wp comment create;
  • update comments with wp comment update;
  • approve comments with wp comment approve;
  • and a lot more.

However, removing all of the spam comments from your website at once can be the most intriguing thing you can do in this situation. This is very useful if you ever discover that your database has hundreds of brand-new spam comments.

The command is this:

wp comment delete $(wp comment list --status=spam --format=ids)

What a clever hack! The ID(s) of the comment(s) that should be deleted is required as an argument for the primary wp comment delete command. Therefore, we are calling another command, wp comment list, which displays a list of all the comments that have been flagged as spam to supply the ID(s).

Tame wp-cron

The standard wp-cron mechanism hasn’t been the most reliable thing. More or less, when you schedule a post in WordPress, you can never be 100% sure that it will indeed go live.

WP-CLI can help you tame cron just a bit with the use of this command:

wp cron event run --due-now

The command triggers all cron events due right now.

Get Help on Any WP-CLI Command

Even though WP-CLI is fantastic, it still uses a text interface, making it challenging to constantly remember which command to execute when.

There are two actions you can take to assist yourself:

The standard help command is first:

wp help COMMAND

This input displays help on any command in WP-CLI.

Next, there’s the –prompt argument that works with most WP-CLI commands. For example, if I attempt to generate some new content like this:

wp post generate --prompt

WP-CLI will list all the arguments that are available for the generate command and let me fill in the blanks.

Conclusion

Many chores that are sometimes regarded as tedious are simplified by WP-CLI to the point that doing them is nearly enjoyable. You may utilize WP-CLI to easily access practically everything you need on your WordPress site, saving you time from having to spend hours getting over roadblocks that are holding you back from addressing the underlying problems.

The circumstances listed above are just a few of the many uses for WP-CLI; until we fully grasp its possibilities, they will only serve as a small portion of its potential value.

Have you got any inquiries concerning WP-CLI? Or perhaps you’d want to share some of your own clever tricks with us. Let us know in the comments section below!

Joseph

Joseph is part of the FastComet Marketing team. With years of content writing experience behind him, it's one of his favorite activities. Joseph takes part in the SEO of the FastComet website and blog. His goal is to write comprehensive posts and guides, always aiming to help our clients with essential information. Joseph also has a thirst for knowledge and improvement, which makes the hosting environment a perfect place for him.