EthicsComputerCourses
Join training +91 90532 10052

WP-CLI: WordPress from the Command Line

WordPress · Lesson 27 of 30 Advanced 7 min read Updated

By the end of this lesson you will install WP-CLI and use it to set up a complete WordPress site with a handful of commands. You will also manage plugins, users and content, back up the database and run a safe search-replace. Tasks that take twenty clicks in the dashboard take one line here.

WP-CLI is the official command-line tool for WordPress. You type commands in a terminal, and it talks to WordPress directly. If the dashboard is like ordering at a restaurant counter, WP-CLI is like calling the kitchen directly with an exact order. If you are new to terminals, the Linux terminal lesson is a gentle start.

Step 1: Install WP-CLI

WP-CLI is a single PHP file called wp-cli.phar. It needs PHP 7.2.24 or newer on your computer or server. Many hosts already have it installed; type wp --info over SSH to check.

On Linux or macOS:

Terminal
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
php wp-cli.phar --info
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
wp --info

On Windows with XAMPP:

  1. Step 1: Make sure php works in Command Prompt (add C:\xampp\php to your PATH if needed).
  2. Step 2: Create the folder C:\wp-cli and save wp-cli.phar there.
  3. Step 3: In the same folder, create wp.bat containing two lines: @ECHO OFF and php "c:/wp-cli/wp-cli.phar" %*.
  4. Step 4: Add C:\wp-cli to your PATH, open a new Command Prompt and run wp --info.

When it works, wp --info prints your operating system, PHP binary and version, and the WP-CLI version. Every command runs on the WordPress site in your current folder, so cd into the site folder first.

Step 2: A whole site in five commands

Watch how quickly a new local site comes together. In an empty folder such as C:\xampp\htdocs\demo:

Terminal
wp core download
wp config create --dbname=demo_local --dbuser=root --prompt=dbpass
wp db create
wp core install --url=http://localhost/demo --title="Demo Coaching" --admin_user=meera --admin_email=meera@example.com
wp core version
Output
Downloading WordPress 7.1.4 (en_US)...
md5 hash verified: (a 32-character checksum)
Success: WordPress downloaded.
Success: Generated 'wp-config.php' file.
Success: Database created.
Admin password: (a random password, shown only once)
Success: WordPress installed successfully.
7.1.4

What happened:

  • core download fetched the latest WordPress and checked it was not damaged.
  • config create wrote wp-config.php. --prompt=dbpass makes WP-CLI pause and ask for the password, so it never appears in the command or your shell history (just press Enter for XAMPP's empty root password).
  • db create created the database named in wp-config.php.
  • core install ran the installer. With no --admin_password, WP-CLI made a strong random one and printed it once; copy it into your password manager.

Step 3: Plugins and themes

Terminal
wp plugin install contact-form-7 --activate
wp theme install twentytwentyfive --activate
wp plugin update --all
wp plugin list --status=inactive --field=name

The last command prints only the names of inactive plugins, one per line, which is handy for cleaning up:

Output
akismet
hello

Other useful commands: wp plugin deactivate <name>, wp plugin delete <name>, wp theme list, wp plugin auto-updates enable <name>.

Step 4: Users and content

Terminal
wp user create priya priya@example.com --role=editor --send-email --porcelain
wp post create --post_type=page --post_title="Contact" --post_status=publish --porcelain
wp post list --post_type=page --fields=ID,post_title,post_status
Output
2
6
+----+--------------+-------------+
| ID | post_title   | post_status |
+----+--------------+-------------+
| 6  | Contact      | publish     |
| 2  | Sample Page  | publish     |
+----+--------------+-------------+

--porcelain prints only the new ID, which scripts can capture. The first line is Priya's user ID; the second is the new page's ID. Your numbers may differ.

You can also change settings: wp option update blogdescription "Maths and Science for Classes 9 to 12".

Step 5: Database backup and search-replace

Export the whole database to a file:

Terminal
wp db export demo-2026-11-02.sql
Output
Success: Exported to 'demo-2026-11-02.sql'.

wp db import demo-2026-11-02.sql loads it back.

When a site moves to a new address, old URLs are stored all over the database. Some are inside serialized PHP data, a format that records the length of each piece of text. A plain SQL find-and-replace changes the text but not the recorded length, which corrupts the data. wp search-replace understands serialized data and fixes lengths correctly.

Always try it first with --dry-run, which reports what would change without changing anything:

Terminal
wp search-replace 'http://localhost/demo' 'https://demo.brightfuture.in' --dry-run --report-changed-only
Output
+------------+--------------+--------------+------+
| Table      | Column       | Replacements | Type |
+------------+--------------+--------------+------+
| wp_options | option_value | 2            | PHP  |
| wp_posts   | post_content | 14           | SQL  |
| wp_posts   | guid         | 9            | SQL  |
+------------+--------------+--------------+------+
Success: 25 replacements to be made.

If the report looks right, run the same command without --dry-run. Many developers add --skip-columns=guid, because the GUID is meant to be a permanent ID, not a link. You will use this in Backups & Migrating a Site.

Step 6: Maintenance commands

CommandWhat it does
wp core updateUpdates WordPress core
wp core verify-checksumsChecks core files against official checksums
wp rewrite flushRebuilds permalink rules
wp cache flushEmpties the object cache
wp transient delete --expiredRemoves expired temporary data
wp cron event listShows scheduled background tasks
wp media regenerate --yesRebuilds image sizes
wp maintenance-mode activateShows a maintenance message to visitors

Type wp help for all commands, or wp help plugin install for one command's options.

A tiny automation script

Because WP-CLI is just commands, you can save a routine in a file. This bash script backs up the database, then updates everything:

Terminal
#!/usr/bin/env bash
set -e
cd /home/brightfuture/public_html
wp db export "../backups/db-$(date +%F).sql"
wp core update
wp plugin update --all
wp theme update --all
wp core verify-checksums

set -e stops the script at the first error, so updates never run if the backup failed. This is the same idea as the Linux bash scripts lesson.

Common mistakes

  • Running a command in the wrong folder. WP-CLI acts on whichever site is in the current folder. Check with wp option get siteurl.
  • Putting passwords in commands. They stay in your shell history. Use --prompt=... or environment variables.
  • Skipping --dry-run before search-replace.
  • Running WP-CLI as the root user on a server; it refuses by default for safety. Use the site's own user.
  • Updating everything on a busy shop without a backup.

Key points

  • WP-CLI is the official command-line tool; install wp-cli.phar and run wp --info to check.
  • core download, config create, db create and core install build a site in minutes.
  • Manage plugins, themes, users, posts and options with short commands; --porcelain prints just IDs.
  • wp db export and wp db import back up and restore the database.
  • wp search-replace handles serialized data; always run --dry-run first.
  • Combine commands into scripts for repeatable maintenance.

Exercise

Write the WP-CLI commands to: 1) create a page titled "Results 2026" as a draft; 2) install and activate the Two Factor plugin; 3) list all administrators' usernames and emails; 4) export the database to before-launch.sql; 5) do a dry run replacing http://localhost/coaching with https://brightfuture.in, skipping the guid column.

Show solution
Terminal
wp post create --post_type=page --post_title="Results 2026" --post_status=draft
wp plugin install two-factor --activate
wp user list --role=administrator --fields=user_login,user_email
wp db export before-launch.sql
wp search-replace 'http://localhost/coaching' 'https://brightfuture.in' --skip-columns=guid --dry-run

The plugin slug comes from its directory address, wordpress.org/plugins/two-factor/. Nothing changes in the database until you run the last command again without --dry-run.

Quick Quiz

1. Which site does a WP-CLI command act on?

2. Why use wp search-replace instead of a plain SQL REPLACE when moving a site?

3. What does --porcelain do on wp post create?

4. What does --dry-run do?

Learn WordPress with a mentor

Free lessons get you started. Our WordPress & Website Building training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call