Get DevWP - WordPress Development Theme
WP Cli Windows MacOS

WP-CLI The Command Line Interface for WordPress

WordPress is used by over 810,000,000 websites. That’s 43%+ of all websites are using WordPress. The nearest competitor is Shopify with less than 4% market share. These numbers highlight the success WordPress has had over the years, growing at a rapid pace and once saturation was reached, smaller but steady growth has continued.

Why do I mention these statistics when the topic of the article is WP-CLI? The answer is simple, that’s a lot of websites that need to be managed and maintained!

Securing a WordPress Website is not a difficult task. You only need to follow a few best practices in order to secure your website from everyday, run of the mill cyber attacks.

  1. Secure Credentials for your website and hosting account.
  2. Use two-factor authentication.
  3. Use a reputable WordPress Theme.
  4. Use reputable WordPress Plugins.
  5. Install Security Plugins and Firewall.
  6. Properly manage user roles and permissions.
  7. Use proper file and folder permissions on your server.
  8. Choose a reputable hosting provider.
  9. Secure your home and work computers and networks.
  10. Stay current on WordPress Security News.
  11. Keep WordPress Core, Themes and Plugins updated!

The last item on that list is what leads us into the topic of WP-CLI which is the Command Line Interface for WordPress.

Checkout the Video:

Managing a WordPress powered website isn’t difficult. The Admin section aka the backend of your website is a clean user interface that makes it easy to go from one section to the next to do various administrative tasks on your website.

The issue is sometimes we don’t have the time to check in on our WordPress site. Yes, if your website is extremely active with new articles being added daily, then logging in is a habit you already have.

But the vast majority of small to medium size WordPress Websites aren’t logged into daily. Sometimes not even weekly. That poses a problem when it comes to keeping these websites secure and up to date.

If you’re managing more than one website that you own, or if you have multiple clients that you manage websites for, then logging into each can be time consuming and tedious.

WP-CLI is a powerful tool that makes it easy to manage a WordPress Website from the command line. What would typically take a significant amount of time, can now be handled in minutes. This frees you up to do other tasks that matter to your business.

What Can WP-CLI do?

With wp-cli, you can do everything you would typically do via a browser from the backend of your website, directly from the command line. This is a game changer when it comes to streamlining your workflow.

Below is a list of some of what’s possible with wp-cli.

  • Downloading WordPress
  • Installing & activating WordPress Themes & Plugins
  • Setting up your wp-config.php file
  • Setting up your database
  • Configure a WordPress Multisite
  • Update WordPress Core, Themes, and Plugins
  • Optimize your database
  • Backup your database
  • Manage WordPress Users
  • Create and manage content like posts and pages
  • Regenerate Thumbnails
  • Query your database
  • Change your WordPress Salts
  • Change your Permalinks
  • and more

That list is some of the basics of what can be done with wp-cli, all without using a browser. This makes it easier and faster to manage a WordPress Website.

Get Started with WP- CLI

After that lengthy introduction, we finally get to the main topic of how to get started with wp-cli. Instead of jumping right in with installing and using wp-cli on your web server, I recommend first getting to know how to use it and get a rock solid understanding of the capabilities of wp-cli.

You can use wp-cli on Windows, macOS, and Linux operating systems and installing it on each OS is similar but there can be some differences depending on your system.

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 terminal and run the following command.


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

Then check to make sure it’s working by entering the following command.


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 y 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 that doesn’t have an extension, wp. You can do so with the following command in terminal. Make sure you’re in the bin folder in terminal.


nano wp

In that file, I place the following code found below.


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

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

Then in terminal, run the following command to make sure it worked.


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.

If you want to create the Bin Directory via terminal, you would use the following command:


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 thing you should do is make sure everything is working properly with the following command:


wp --info

Then you can easily download the latest version of WordPress using wp-cli in your terminal which is significantly faster than going through the browser.

All you have to do is make sure you’re in your htdocs folder and your server is running. Then make an empty folder with the mkdir command:


mkdir wptest

Then you will enter that directory via terminal and paste the following command:


wp core download

That command will download WordPress Core files and folders into that folder.

You can also check the version of WordPress with the following command:


wp core version

You can check if there’s an update available with this command:


wp core check-update

If you want to update WordPress, you can use the following command:


wp core update

You can Rollback WordPress to a Previous Version:


wp core update --version=6.0.3 --force

WP-Config.php file and WP-CLI

Use the following command to generate your config file in the root of your WordPress Website:


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

WordPress Database via WP CLI

Create the database


wp db create --path="$WP_DIR"

If you want to install a WordPress Theme, you can use the following command and just make sure to use the slug for the theme you want to use:


wp theme install twentytwenty

The previous command only installed the theme, if you want to activate the theme when you install it, you would do so with the following command:


wp theme install twentytwentyone --activate

You can install a group of WordPress Plugins with ease by using the following command:


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 

The previous code snippet was to install several developer plugins I like to use when developing WordPress Themes. Basically you just need the slug of the plugin you want to install and then make sure they are separated by a space.

You can activate all the plugins in one shot with this command:


wp plugin activate --all

You can also deactivate all the plugins like so:


wp plugin deactivate --all

If you want to delete a plugin, use the following command and make sure to use the slug of the plugin you want to delete:


wp plugin delete akismet

You can update all the plugins with the following command:


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 you want to backup the database to and then run the following command:


wp db export backup.sql

Here are a few more commands that you can use to do various tasks with your database from checking the size, optimizing the database, and repairing the database.


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.

Another way to empty your website from the default content is to use the following command:


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.

Use the following commands to help you generate some content.


wp post generate --count=20

The command above will just generate 20 posts without content which is not really helpful. You can use the following command to add some demo content to the empty posts.


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

You can view all the users on your website 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

Permalinks Management via WP-CLI

You can easily change your permalink structure by using wp-cli in your terminal. Use the following command to help you change your permalinks.


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.


#!/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!"


View Our Themes