Get DevWP - WordPress Development Theme
WP-CLI The Command Line Interface for WordPress
WP-CLI is the official command-line interface for WordPress. Manage themes, plugins, databases, and users from your terminal — plus automate full WordPress installs with a single Bash script.

WP-CLI The Command Line Interface for WordPress

9 min read

WordPress powers over 810 million websites — and every one of them needs updates, backups, and maintenance. If you’re managing more than one site, logging into each dashboard gets tedious fast.

WP-CLI is the official command-line interface for WordPress. What takes clicks and page loads in the browser becomes a single terminal command. This post covers installation on Windows and macOS, the most useful commands, and a Bash script that spins up a complete WordPress install in seconds. WP-CLI also handles multisite setup, user management, and database maintenance. Check the official WP-CLI site for full documentation and the latest version.

System Requirements

Before installing WP-CLI on your computer or server, you will need to make sure it can run properly on your system.

  • Unix like system – macOS and Linux will work out of the box, but for Windows you will need to either use Windows Subsystem for Linux or use Git-Bash which comes with Git. My preference is using Git-Bash.
  • PHP 5.6 or later.
  • A recent version of WordPress is preferred.

Download WP-CLI Phar File

If you want to follow the standard method of installing WP-CLI then follow these steps. If you’re on macOS and want to use Homebrew, then skip this step.

Open your terminal and download the phar file:


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

Then verify it’s working:


php wp-cli.phar --info

Use wp instead of php wp-cli.phar

If you want to use wp command instead of the full command php wp-cli.phar, then you will need to make the file executable and move it into your path.

Windows Specific Path Setup

On my system what I did was create a bin folder in my user directory /c/Users/YourUserAccount/bin. In that bin folder is where I downloaded the phar file. If you already downloaded the file then you could just move the file into that folder.

You will then want to make the file executable if it’s not already.


chmod +x wp-cli.phar

Now when it comes to moving the bin folder into your path, you can hit the Windows Key and type out environment variable.

From there you would click on Advanced, then environment variables, and then in your user variables, look for path and edit that. Browse for your directory and add it to your path. Then click ok.

I also created a file with no extension called wp. Make sure you’re in the bin folder, then create it:


nano wp

In that file, place the following:


#!/usr/bin/env sh
dir=$(dirname "$0")
php "${dir}/wp-cli.phar" "$@"

Then save the file by clicking control X and then Y.

Then verify it works:


wp --info

macOS Setup and Configuration

On macOS, I decided to use HomeBrew to streamline the process of installing and working with WP-CLI. HomeBrew is the missing package manager for macOS and Linux.

If you don’t already have HomeBrew Installed on your system, then you can use the command below in terminal.


/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

MAMP and MySQL

I also needed to make sure that MySQL was in my path in order for things to work properly. You can use the commands below for either ZSH or BASH environments.

For ZSH environments:


echo 'export PATH="/Applications/MAMP/Library/bin:$PATH"' >> ~/.zshrc

For BASH environments:


echo 'export PATH="/Applications/MAMP/Library/bin:$PATH"' >> ~/.bashrc

Bin Directory in htdocs folder

I also decided to create a bin directory in my htdocs folder so I could place the Bash Script file that is used to automate a lot of our commands.

Create the directory from terminal:


mkdir bin

I’ll come back to this directory in the general section where we create the file to be placed in there.

Download WordPress with WP-CLI

First, confirm WP-CLI is working:


wp --info

Downloading WordPress via WP-CLI is significantly faster than the browser. Make sure you’re in your htdocs folder with your server running, then create a new directory and download the core files:


mkdir wptest

Enter that directory in terminal and download WordPress:


wp core download

Once WordPress is downloaded, here are the core management commands you’ll use regularly:


# Check installed version
wp core version

# Check for available updates
wp core check-update

# Update to latest version
wp core update

# Rollback to a specific version
wp core update --version=6.0.3 --force

WP-Config.php file and WP-CLI

Generate your config file in the root of your WordPress site:


wp config create --dbname=<name> --dbuser=<user> --dbpass=<password>

WordPress Database via WP-CLI

Create the database:


wp db create --path="$WP_DIR"

Installing Themes and Plugins

To install a WordPress theme, use its slug. Add –activate to activate it immediately:


wp theme install twentytwenty

wp theme install twentytwentyone --activate

For plugins, you can install multiple at once by separating slugs with spaces. Here’s my go-to list of developer plugins:


wp plugin install debug-bar debug-bar-actions-and-filters-addon classic-editor default-featured-image plugin-inspector log-deprecated-notices query-monitor theme-check wordpress-beta-tester show-current-template simply-show-hooks theme-inspector view-admin-as

Once installed, managing plugins is straightforward:


# Activate all installed plugins
wp plugin activate --all

# Deactivate all plugins
wp plugin deactivate --all

# Delete a specific plugin by slug
wp plugin delete akismet

# Update all plugins
wp plugin update --all

Database Backup with WP-CLI

It’s always a good idea to take a backup of your database, and while there’s a lot of plugins that can help you with this, WP-CLI comes to the rescue with a one line command to simplify the process.

Navigate to the folder where you want the backup saved, then export:


wp db export backup.sql

More database management commands:


wp db size

wp db size --tables

wp db tables

wp db optimize

wp db repair

wp db check

wp transient delete --all

wp transient delete-expired

wp db reset

Be careful with the wp db reset command since it will reset your database.

You can also wipe all default content entirely:


wp site empty

Generate Demo Content

If you’re developing a website locally, or on a staging environment, you may want to have some demo content to help you visualize the layouts of your site.

The basic command generates empty posts, which isn’t very useful:


wp post generate --count=20

Pipe in some Lorem Ipsum to give them actual content:


echo -e "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." | wp post generate --post_content --count=20

That command will give you 20 posts with some Lorem Ipsum.

User Management with WP-CLI

WP-CLI makes user role management simple. View all users on your site with this command:


wp user list

List users with administrator role


wp user list --role=administrator

Create a new subscriber user


wp user create john john@smith.com --role=subscriber

Delete user john and reassign posts to user jane


wp user delete john --reassign=jane

Changing your permalink structure is a one-liner:


wp rewrite structure '%postname%'

Automate WP-CLI with Bash Scripts

Now the fun really begins. Combining the commands that make managing WordPress a breeze, with Bash Scripts, will make you extremely productive.

Bash scripting is a powerful way to automate your workflow and I highly recommend you spend some time getting familiar with scripting in Bash.

For brevity, I will share the script I created that should go in a file named install-wp.sh. You can actually name it whatever you want, but that’s the name I chose.

Note: if you’re using ZSH for your shell environment, you will need to change the shebang on the first line of the script to


#!/bin/zsh

The rest of the script stays the same whichever environment you’re running.

The credentials below are for local development only. Never use weak passwords like “root” or empty database passwords on a live server.

#!/bin/bash

# Define variables
DB_NAME="wpnew"
DB_USER="root"
DB_PASS=""
DB_HOST="localhost"
WP_DIR="/c/xampp/htdocs/wpnew"
SITE_URL="localhost/wpnew"
SITE_TITLE="WP New"
ADMIN_USER="demo"
ADMIN_PASSWORD="root"
ADMIN_EMAIL="j@mail.com"

# Download WordPress
wp core download --path="$WP_DIR"

# Create the wp-config.php file
wp config create --dbname="$DB_NAME" --dbuser="$DB_USER" --dbpass="$DB_PASS" --dbhost="$DB_HOST" --path="$WP_DIR"

# Set debugging to true
wp config set WP_DEBUG true --raw --path="$WP_DIR"

# Create the database
wp db create --path="$WP_DIR"

# Install WordPress
wp core install --url="$SITE_URL" --title="$SITE_TITLE" --admin_user="$ADMIN_USER" --admin_password="$ADMIN_PASSWORD" --admin_email="$ADMIN_EMAIL" --path="$WP_DIR"

# Install plugins
wp plugin install debug-bar debug-bar-actions-and-filters-addon classic-editor default-featured-image plugin-inspector log-deprecated-notices query-monitor theme-check wordpress-beta-tester show-current-template simply-show-hooks theme-inspector view-admin-as --path="$WP_DIR"

# Activate plugins
wp plugin activate --all --path="$WP_DIR"

# Delete default plugins
wp plugin delete akismet hello --path="$WP_DIR"

# Delete default post and page
wp post delete 1 --force --path="$WP_DIR" # deletes the "Hello world!" post
wp post delete 2 --force --path="$WP_DIR" # deletes the "Sample Page"

# Generate default content
echo -e "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." | wp post generate --post_content --count=20 --path="$WP_DIR"

# Set permalink structure
wp rewrite structure '%postname%' --path="$WP_DIR"

wp user create JohnDoe john@mail.com --role=subscriber --user_pass=password123 --display_name="John Doe" --path="$WP_DIR"
wp user create JaneSmith jane@mail.com --role=subscriber --user_pass=password123 --display_name="Jane Smith" --path="$WP_DIR"

echo "WordPress installed successfully!"

WP-CLI FAQ

Does WP-CLI work with MAMP and XAMPP?

Yes, as long as PHP and MySQL are in your system PATH. See my guides on MAMP for macOS and XAMPP for Windows for PATH setup instructions.

Can WP-CLI manage remote servers?

Yes, via SSH. Use wp --ssh=user@host to run commands on a remote WordPress install without logging in to the server directly.

Once you have that script saved, you can spin up a fresh WordPress install with a single command. I use WP-CLI on every project now — it’s one of those tools that once you start using, you wonder how you managed without it.



View Our Themes