Help Center
A Guide to WP-CLI: Use Cases and Tips for Beginners
bulat
Alexander Bulat
WordPress Copywriter
Show all articles
Updated on
Useful Resources

A Guide to WP-CLI: Use Cases and Tips for Beginners

You probably know the WordPress admin dashboard, but what if you could skip the graphical interface and manage your sites way faster and more effectively? This is where WP-CLI steps in – a developer’s tool to help manage WordPress sites through terminal commands. This article will introduce WP-CLI and showcase a few interesting cases, along with tips for getting started.

Table of Contents

What Is WP-CLI?

WP-CLI is a powerful tool that lets you perform many WordPress tasks from the command line. You will be able to install plugins, manage users, and even update WordPress core. It’s especially helpful in the case when some developers are working on several sites at once; it saves their time because there is no need to click something in the admin dashboard.

For example, instead of having to log into your WordPress admin panel and update plugins one by one, you can run a single command in the terminal to bulk update all plugins.

Why Should One Use WP-CLI?

To developers, WP-CLI offers speed and the ability to automate. This is really helpful when one needs to update WordPress core on dozens of sites, manage database optimizations, or do anything that requires repetition. It’s equally good to set up sites quickly, perform backups, and manage users – all without the tedium of using the WordPress admin interface.

Download and Install WP-CLI

Before diving into the awesome power of WP-CLI, you’ll need to get it installed. The installation process is straightforward, but there are a few things to consider. Here’s a step-by-step guide. 

NOTE

In case you’re installing WP-CLI on your hosting server and not on localhost, you need to ensure that SSH is enabled. To enable SSH, you need access to your hosting account, cPanel, where you can activate SSH access. If you’re unsure how to enable SSH, consult your hosting provider’s documentation or support team. Once SSH is enabled, you can proceed with installation.

Download WP-CLI

Use the command that downloads the WP-CLI Phar (PHP Archive) file on your server:

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

Verify the download

It’s always good to verify that the file was downloaded correctly and is executable. You can check by running the following command:

php wp-cli.phar --info

If everything is correct, you’ll see WP-CLI’s version and other information.

Make WP CLI globally accessible

To make WP-CLI easier to use, you can make it a global command by moving the downloaded file and renaming it:

chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp

Now, instead of typing php wp-cli.phar, you can simply use wp from anywhere in your terminal.

Confirm installation

Finally, confirm that WP-CLI is installed correctly; this command will output details about your WP-CLI installation, ensuring it’s ready for use:

wp --info

NOTE

Mac users might have to complete more steps during installation since WP-CLI requires additional components that may not be available on your machine, like Homebrew, PHP, Curl, and MySQL. You’re recommended to check out this comprehensive video review on WP-CLI installation on Windows and macOS.

Tips for Getting Started with WP-CLI

Before using WP-CLI on a live site, experiment with a local installation of WordPress. This will give you the chance to get comfortable with the commands without risking live data. WP-CLI supports command autocompletion, which can save you time by predicting what you’re going to type next. To enable this, follow the instructions.

Use wp help

WP-CLI has built-in documentation for every command. If you’re ever unsure about a command or its parameters, you can use:

wp help <command>

For example, to get help with the `plugin` command:

wp help plugin

Alternatively, you can just type: 

wp

…to get a list of all subcommands.

list of wp subcommands

Safe mode with –dry-run

If you’re running a command that modifies the database or site settings and you’re worried about breaking something, use the `–dry-run` flag. This simulates the command without actually making any changes:

wp search-replace 'oldurl.com' 'newurl.com' --dry-run

Common Use Cases

Here are some of the most common and useful things you can do with WP-CLI.

Installing WordPress

Instead of going through the manual installation, use this sequence of commands; it downloads WordPress, creates the configuration file, and installs WordPress with your desired settings:

wp core download

wp config create --dbname=your_db_name --dbuser=your_db_user --dbpass=your_db_password

wp core install --url="http://example.com" --title="Your Site Title" --admin_user="admin" --admin_password="password" --admin_email="[email protected]"

NOTE

For more information on the WordPress core installation command options, you can refer to the WordPress Dev Resources.

Managing plugins and themes

WP-CLI makes it easy to install, activate, and update plugins and themes. Here’s how you can install and activate a plugin:

wp plugin install contact-form-7 --activate

Or update all plugins on your site at once:

wp plugin update --all

You can also similarly manage themes:

wp theme install twentytwenty --activate

wp theme update --all

Database management

Looking to back up your database or do a WP-CLI search replace after moving a site? WP-CLI handles these tasks seamlessly:

wp db export backup.sql

To perform a search and replace (for example, after migrating a site):

wp search-replace 'oldurl.com' 'newurl.com' 

This updates all instances of the old URL with the new one in the database.

Updating WordPress core

Keeping WordPress up to date is crucial for security and performance. WP-CLI allows you to update WordPress core with a single command:

wp core update

This ensures that your WordPress installation always runs the latest version without you needing to log into the admin dashboard.

User management

WP-CLI can also simplify user management. You can create new users, update existing ones, or reset passwords. For instance, use this command to create a new user:

wp user create newadmin [email protected] --role=administrator --user_pass=strongpassword

If a user forgets their password, you can reset it easily:

wp user update admin --user_pass=newpassword

Generating random posts for testing

Sometimes, when you are setting up a new site or theme, you need content just to see what things look and feel like on the front end. Creating posts by hand is a pain, so WP-CLI can create random posts for you for testing purposes. You can quickly populate your site with dummy content using this simple command:

wp post generate --count=20 --post_type=post

Of course, you can change the number of posts to any that you want. You can even set other parameters, like categories, post status, or author.

This command will generate 50 published pages with a specific author. It’s an excellent way to fill your site with data to test layouts and plugins or even just for prototyping:

wp post generate --count=50 --post_author=2 --post_type=page --post_status=publish

Advanced Use Cases

For developers who want to go beyond the basics, WP-CLI has advanced features that can be incredibly useful.

Custom commands

One of WP-CLI’s best features is the ability to create custom commands. This allows you to automate processes that fit your workflow. For example, you can create a script that automatically backs up your database and sends it to a remote server:

wp db export

scp backup.sql user@server:/path/to/destination

Clearing transients

WordPress uses transients to store cached information in the database. Over time, these can pile up and affect performance. WP-CLI makes it easy to clear them with a single command:

wp transient delete --all

If you want to clear a specific transient, you can do so by name:

wp transient delete your_transient_name

It’s a good practice to run this command now and then or even set it up as a cron job to keep the database running smoothly without intervention.

Bulk deleting spam comments

Every owner of a WordPress site knows how fast spam comments can build up. Well, the good thing with WP-CLI is that you can delete all spam comments with just one command. This command finds all the comments marked as spam and then deletes them. It’s one simple way to clean up the database and make moderation easier:

wp comment delete $(wp comment list --status=spam --field=ID)

Enabling/disabling Maintenance mode

When updating plugins or making changes to your site, you may want to put WordPress in maintenance mode to prevent visitors from seeing incomplete updates. WP-CLI provides a quick way to turn maintenance mode on or off:

wp maintenance-mode activate

To deactivate maintenance mode:

wp maintenance-mode deactivate

This is especially useful during scheduled maintenance, ensuring users don’t stumble upon a half-updated site.

Instant WordPress multisite creation

Setting up a multisite network manually can be tedious, but with WP-CLI, you can automate the whole process in minutes. Whether you want to create a network for a client or spin up a personal multisite for testing, WP-CLI makes it incredibly easy:

wp core multisite-install --url=example.com --title="My Network" --admin_user=superadmin --admin_password=strongpassword [email protected] --subdomains

This command sets up a multisite with subdomains (or you can omit –subdomains to create a network with subdirectories). WP-CLI automatically handles all the steps for creating and configuring your multisite network. You can even add new sites to the network with a single line:

wp site create --slug=subsite --title="Sub Site" [email protected]

Automated content scraper for WordPress

If you need to pull in content from external sources and populate your WordPress site, you can build a custom WP-CLI command that scrapes content from an external website and publishes it to your WordPress site automatically. Let’s say you want to pull in news articles:

wp eval-file content-scraper.php 

Where content-scraper.php might look like this:

<?php
$url = 'https://newswebsite.com/rss-feed';
$rss = simplexml_load_file($url);
foreach ($rss->channel->item as $item) {
    wp_insert_post([
        'post_title'   => (string) $item->title,
        'post_content' => (string) $item->description,
        'post_status'  => 'publish',
        'post_author'  => 1
    ]);
}

This script pulls in the latest news articles from an RSS feed and publishes them as posts. You can tweak this script to fit your exact needs, pulling content from APIs, other sites, or anywhere else.

Roll back core, plugins, or themes to any previous version

WP-CLI allows you to roll back to an earlier version of WordPress core, themes, or plugins without needing backups or extra hassle.

Rolling back WordPress core:

wp core update --version=5.6

Rolling back a plugin (e.g., WooCommerce):

wp plugin install woocommerce --version=4.5.0 --force

You can do the same for themes. This feature is a lifesaver when an update breaks functionality on your site, and you need to revert quickly.

Top Resources to Master WP-CLI

Whether you’re just getting started with WP-CLI or want to take your knowledge to the next level, there are many fantastic resources available. Below is a curated list of the very best resources that will walk you through how to master WP-CLI commands; each describes what you can learn from it.

WP-CLI Handbook

The official WP-CLI handbook is a must-read for anyone serious about learning WP-CLI. It covers everything from basic commands to advanced topics such as custom command creation, integrating WP-CLI into CI/CD pipelines, and more. It’s well-organized and perfect for both beginners and advanced users.

WP-CLI GitHub Repository

For developers who like to dig into the code, the official WP-CLI GitHub repository is a fantastic resource. You can explore the codebase, report issues, contribute to the project, and even check out community-developed packages to extend WP-CLI’s functionality.

WP-CLI Blog

The official WP-CLI blog provides updates, tutorials, and case studies on WP-CLI usage. It’s a fantastic resource for keeping up with the latest features and best practices as the tool evolves. The blog often highlights interesting community contributions and real-world applications.

WP Sessions: Writing WP-CLI Commands That Work

This free session from WP Sessions offers a deep dive into writing effective WP-CLI commands. It’s a great resource for those who want to extend WP-CLI’s functionality by creating custom commands that fit their development workflow.

Delicious Brains: Installing, Updating, and Managing Plugins with WP-CLI

This article by Delicious Brains focuses on one of the most practical aspects of WP-CLI: plugin management. You’ll learn how to install, update, and manage plugins efficiently using WP-CLI, which is particularly useful for managing multiple WordPress sites.

AgentWP: 9 Best WP-CLI Packages for WordPress Development

This blog post highlights some of the most useful WP-CLI packages for WordPress development. These packages extend WP-CLI’s capabilities, allowing you to perform tasks like better database management, deployment, and performance optimization.

FAQ

What is WP-CLI, and why should I use it?

WP-CLI is a command line interface for WordPress that enables developers to do almost everything that can be done within WordPress from within the terminal. This includes everything from WordPress installation, plugin and theme management, and optimization of the database to creating posts and reaching user management. It saves a lot of time and is definitely more efficient than using the WordPress admin interface for repeated tasks on multiple sites or automated processes.

Can I use WP-CLI on shared hosting?

Yes, you can run WP-CLI on shared hosting so long as your hosting provider gives you SSH access. Most popular hosting providers support SSH access, such as SiteGround, Bluehost, and DreamHost. Before using WP-CLI, make sure SSH is enabled and you have permission to be running commands from the command line.

Is WP-CLI safe to use on a live site?

Like with any powerful tool, there are some cautions to confidently using WP-CLI on a live site. Any commands making changes to a database or file system should be used with care on live sites. In general, it can be good practice to back up your database and files before running commands that might affect your site’s content or configuration. Besides, some of the commands support a mode of “dry run”, which will not change anything, just emulate the effect. For example, this can be invoked on plugin installation or theme installation.

Conclusion

WP-CLI is such a powerful tool that has made WordPress development easier and more systematic, from installing WordPress to managing plugins, updating the core, and even automating tasks with custom commands. It helps make it easier for developers to work on repetitive and complex tasks. By incorporating WP-CLI into your workflow, you can save time, reduce manual effort, and be more productive on bulk sites.

If you’re not used to WP-CLI, then try running it first on a local setup. This flexibility will give you great ease while developing.