WP-CLI: WordPress from the Command Line
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:
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 --infoOn Windows with XAMPP:
- Step 1: Make sure
phpworks in Command Prompt (addC:\xampp\phpto your PATH if needed). - Step 2: Create the folder
C:\wp-cliand savewp-cli.pharthere. - Step 3: In the same folder, create
wp.batcontaining two lines:@ECHO OFFandphp "c:/wp-cli/wp-cli.phar" %*. - Step 4: Add
C:\wp-clito your PATH, open a new Command Prompt and runwp --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:
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 versionDownloading 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 downloadfetched the latest WordPress and checked it was not damaged.config createwrotewp-config.php.--prompt=dbpassmakes 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 createcreated the database named inwp-config.php.core installran 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
wp plugin install contact-form-7 --activate
wp theme install twentytwentyfive --activate
wp plugin update --all
wp plugin list --status=inactive --field=nameThe last command prints only the names of inactive plugins, one per line, which is handy for cleaning up:
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
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_status2 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:
wp db export demo-2026-11-02.sqlSuccess: 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:
wp search-replace 'http://localhost/demo' 'https://demo.brightfuture.in' --dry-run --report-changed-only+------------+--------------+--------------+------+ | 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
| Command | What it does |
|---|---|
wp core update | Updates WordPress core |
wp core verify-checksums | Checks core files against official checksums |
wp rewrite flush | Rebuilds permalink rules |
wp cache flush | Empties the object cache |
wp transient delete --expired | Removes expired temporary data |
wp cron event list | Shows scheduled background tasks |
wp media regenerate --yes | Rebuilds image sizes |
wp maintenance-mode activate | Shows 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:
#!/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-checksumsset -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-runbefore 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.pharand runwp --infoto check. core download,config create,db createandcore installbuild a site in minutes.- Manage plugins, themes, users, posts and options with short commands;
--porcelainprints just IDs. wp db exportandwp db importback up and restore the database.wp search-replacehandles serialized data; always run--dry-runfirst.- 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
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-runThe 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?
WP-CLI looks for WordPress in the current folder unless told otherwise.
2. Why use wp search-replace instead of a plain SQL REPLACE when moving a site?
Plain SQL breaks serialized values whose lengths change.
3. What does --porcelain do on wp post create?
It gives a clean, script-friendly output.
4. What does --dry-run do?
A dry run is a safe preview.
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
