I use WP-CLI every day. Installing plugins, running database backups, updating multiple sites – all from a single terminal window. At first glance, it might seem like the admin dashboard is fast enough. But once you start using the command line for WordPress tasks, there is no going back.
WordPress provides a graphical interface for every administrative task, which is actually why it is the most popular content management system on the web. But in terms of efficiency, working with the command line allows you to perform many actions more quickly and conveniently than from the WordPress management system.
WP-CLI is the official command-line tool for managing WordPress. This guide covers everything from installation to advanced operations like database management, user administration, and remote server aliases.
Installing WP-CLI
Note: The following steps require working in a UNIX-like environment (macOS, Linux, or FreeBSD). If you are using Windows, the recommended approach is to use Windows Subsystem for Linux (WSL).
Installing WP-CLI is simple. The basic idea is to download a PHP file and place it in a directory where it is accessible from anywhere. You can download the WP-CLI script from GitHub.
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
Then you need to allow this file to be executable:
chmod +x wp-cli.pharThe last step is to move this file to a directory so you can run it from anywhere. Changing the file name to a shorter name is also a good idea, which is why WP is the name usually used:
sudo mv wp-cli.phar /usr/local/bin/wpThat’s it. Verify the installation by running:
wp --info
If your installation was successful you should see something like this:
PHP binary: /usr/bin/php
PHP version: 8.2.15
php.ini used: /etc/php/8.2/cli/php.ini
WP-CLI root dir: /home/user/.wp-cli
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 2.12.0
You can also install WP-CLI using alternative methods:
- Via Homebrew (macOS/Linux):
brew install wp-cli - Via Composer:
composer global require wp-cli/wp-cli-bundle - Via Docker: Use the official
wordpress:cliDocker image
To update an existing WP-CLI installation, simply run:
wp cli updateCommon Operations with WP-CLI
Let’s start with the basics and work our way up.
Installing a WordPress Site
Tip: WP-CLI’s
corecommand group provides tools to manage the WordPress installation itself, including downloading, configuring, and installing WordPress.
The first step in creating a new WordPress site is to download the WordPress files. Go to the directory where you want to install WordPress and run the command:
wp core downloadThis command downloads the latest version of WordPress in English (en_US). If you want to download a different version or language, use the --version or --locale parameters.
For example, to download version 6.7 in Hebrew, run:
wp core download --version=6.7 --locale=he_ILOnce the download is complete, create the wp-config.php file using the core config command:
wp core config --dbname=databasename --dbuser=databaseuser --dbpass=databasepassword --dbhost=localhost --dbprefix=prfx_This command generates a configuration file using the database connection details you provide.
The final step is to install WordPress using the core install command:
wp core install --url="example.com" --title="WordPress Website Title" --admin_user="admin_user" --admin_password="admin_password" --admin_email="admin@example.com"Updating WordPress Version
If your version of WordPress needs updating, use the wp core update and wp core update-db commands to update the source files and database (if necessary).
Note: Keeping your WordPress installation up-to-date is critical, especially when it comes to WordPress security fixes and patches.
To perform updates, use:
wp core update
wp core update-dbYou can always check your current WordPress version with:
wp core versionWhile these actions may seem no faster than the admin UI, WP-CLI becomes incredibly efficient when managing multiple sites. If you prefer to disable automatic WordPress updates and handle them manually, WP-CLI is the cleanest way to do it. You can create a script to update them all at once:
#!/bin/bash
declare -a sites=('/var/www/wp1' '/var/www/wp2' '/var/www/wp3')
for site in "${sites[@]}";
do
wp --path="$site" core update
doneIn any case, it is highly recommended to backup the database before any update:
wp db export backup.sqlTip: Starting with WordPress 6.6, WP-CLI includes a built-in wp maintenance-mode command that lets you activate and deactivate maintenance mode directly from the command line – useful during deployments and updates.
Managing Plugins
Managing plugins is equally straightforward. For example, the command wp plugin status shows a table with information about installed plugins and their status:
+---------------------------+----------+-----------+---------+
| name | status | update | version |
+---------------------------+----------+-----------+---------+
| smooth-scroll-up | inactive | available | 0.8.9 |
| wordpress-beta-tester | inactive | none | 1.0 |
| wordpress-importer | active | none | 0.6.1 |
| wpcli-commands | active | none | 1.0 |
+---------------------------+----------+-----------+---------+Here:
– active = plugin is enabled
– inactive = plugin is installed but not active
– available = an update is available for the plugin
You can also install, activate, deactivate, delete, update, and search for plugins using commands like:
wp plugin install wordpress-importer --activate
wp plugin deactivate wordpress-importer
wp plugin delete wordpress-importer
wp plugin update --all
wp plugin search importManaging Themes with WP-CLI
Generally, the same commands used for managing plugins can be applied to themes by replacing the word ‘plugin’ with ‘theme’, so there’s no need to elaborate further. However, one command worth mentioning is scaffold, which creates an empty child theme and shortens the process:
wp scaffold child-theme my-child-theme --parent_theme=twentytwentyfive --theme_name='My Child Theme' --activateUser Management with WP-CLI
Managing users from the command line is one of the most practical WP-CLI features, especially when you’re locked out of the admin panel or need to bulk-manage accounts.
List all users on the site:
wp user listCreate a new administrator:
wp user create john john@example.com --role=administrator --user_pass=securepass123Update an existing user’s role or email:
wp user update 2 --role=editor
wp user update 2 --user_email=new@example.comDelete a user and reassign their content to another user:
wp user delete 4 --reassign=1Resetting an Admin Password
One of the most common scenarios – you forgot the admin password and can’t access wp-admin. With SSH access and WP-CLI, it takes one command:
wp user update 1 --user_pass=newpassword123You can also generate a random password:
wp user reset-password 1This sends a password reset email to the user’s registered address.
Manipulating Information with WP-CLI
Apart from simple commands like post create, post edit, and post delete, WP-CLI provides powerful tools for managing posts. If you need finer control over queries in your theme, check out how to modify the WordPress loop with pre_get_posts.
If you need 400 posts to test your plugin’s code or the theme you created, you can use the command:
wp post generate --count=400It’s also possible to export information and import it from another WordPress site. To do this, you need to install the wordpress-importer plugin:
wp plugin install wordpress-importer --activateNow you can use the export and import commands to complete the task:
wp export
wp import test.xml --authors=createManaging Post Revisions
By default, WordPress stores all post revisions in the database, which inevitably leads to a lot of data in the posts table of the database.
To address this issue, you can use a WP-CLI extension called wp-revisions-cli which enables functionality to manage post revisions. You can install this extension like any WordPress plugin, and then you’ll have the option to use commands such as clean, list, and status.
Managing Media with WP-CLI
The WordPress media library is fine for uploading a few images, but falls apart when you need to bulk import or regenerate thumbnails across hundreds of posts. WP-CLI handles both.
Importing Images
I once encountered a situation where a client provided me with a large number of images and asked me to import them into his WordPress site. Performing this through the management interface is a tedious task.
You can use the media tool of WP-CLI to perform this in one command:
wp media import images_folder/*Regenerating Images
You’ll need to regenerate the thumbnails of the images once you set a new image size in the development process. Of course, you can use a plugin to perform this such as regenerate-thumbnails, but with WP-CLI you can do it quickly and simply in one command:
wp media regenerateThere are countless options for this command since you can chain several commands and choose which images to perform the action on.
For example, to perform the action on the featured images of posts in a specific category, you can use the following command:
wp media regenerate $(wp eval 'foreach( get_posts(array("category" => 2,"fields" => "ids")) as $id ) { echo get_post_thumbnail_id($id). " "; }')Database Operations
WP-CLI gives you direct access to the WordPress database without opening phpMyAdmin.
For example, a simple query can be performed by:
wp db query "SELECT id FROM wp_users;"
In addition, operations such as import, export, and optimization of the database can be performed using the commands:
wp db export
wp db import backup.sql
wp db optimizeIn the export operation, you can also use a script or through a cron job to ensure that the database is backed up automatically and on a schedule of your choice.
Search & Replace
Building a WordPress site in a local or development environment and transferring it to an online server upon completion is an action many of us have performed frequently. Copying files and importing the database are the easy parts.
Replacing old URLs with new ones in the database, however, is a tricky operation because these URLs also exist in serialized data, which a regular search and replace will not recognize.
WP-CLI helps you perform this operation. The search-replace command replaces the old URLs with new ones in all database fields including those containing serialized data.
wp search-replace 'dev.example.com' 'www.example.com'If you want to know the number of URLs you are looking for in the database, you can use the previous command and add the --dry-run parameter:
wp search-replace --dry-run 'dev.example.com' 'www.example.com'
The result will be something like this:
+---------------------+-----------------------+--------------+------+
| Table | Column | Replacements | Type |
+---------------------+-----------------------+--------------+------+
| wpcli_options | option_value | 2 | PHP |
| wpcli_posts | post_content | 1 | SQL |
| wpcli_posts | guid | 28 | SQL |
+---------------------+-----------------------+--------------+------+Configuration and Maintenance
WP-CLI can also modify wp-config.php safely and manage WordPress cron jobs – two tasks that are cumbersome through the admin UI.
Managing wp-config.php
Read a config value:
wp config get DB_HOSTSet or update a config constant:
wp config set WP_DEBUG true --raw
wp config set WP_MEMORY_LIMIT '256M'The --raw flag tells WP-CLI to write the value without quotes, which is required for boolean values like true and false.
Cron Job Management
WordPress uses its own cron system (WP-Cron) to schedule tasks like publishing scheduled posts and checking for updates. WP-CLI lets you inspect and trigger these events:
wp cron event listManually trigger a specific scheduled event:
wp cron event run wp_version_checkRun all due cron events at once:
wp cron event run --due-nowWP-CLI Configuration with wp-cli.yml
If you find yourself typing the same flags repeatedly, create a wp-cli.yml file in your project root to set defaults:
path: /var/www/html
url: https://example.com
user: admin
@staging:
ssh: user@staging.example.com
path: /var/www/stagingWith aliases like @staging, you can run commands on remote servers directly:
wp @staging plugin update --allFAQs
Common questions about WP-CLI:
search-replace, db import, or bulk updates. Use the --dry-run flag when available to preview changes before applying them.wp cli update to update to the latest stable version. You can check your current version with wp cli version. If you installed WP-CLI via Homebrew or Composer, use the respective package manager to update instead.--path flag to target a specific WordPress installation, or write a bash script that loops through multiple site directories. This is one of WP-CLI's biggest advantages for agencies and developers managing many sites.In Conclusion
This guide covers the commands I use most often, but WP-CLI has dozens more. The full list is available on the official site.
Once you get comfortable with the basics, try combining commands in bash scripts. That’s where WP-CLI really pays off – managing ten sites takes the same effort as managing one.

