How to Use WP-CLI to Manage WordPress (+ More)

WP-CLI enables to use the command-line interface to manage WordPress websites. In this article, let’s see how to install WP-CLI and some of its basic commands.

WP-CLI is the command-line interface for WordPress. You might have used the command line or terminal on your computer already. Basically, it lets you perform admin tasks on your WordPress websites that you generally do from the WordPress dashboard.

This might sound a bit inconvenient as you can already perform these tasks from the dashboard, why go through the extra trouble of using a command-line interface. Well, it does have some perks. 

For example, it becomes increasingly difficult when you have multiple WordPress websites. To manage these websites, you have to log in to these dashboards separately. There are lots of extra mouse clicks. This is where WP-CLI becomes more convenient.

There are some other advantages of using the command line interface like you can create your own custom commands to execute a process that is specific to your WordPress website only. If you want to know more about this topic then keep on reading. In this post, we will be discussing what WP-CLI is, how to install it, how to use WP-CLI, some commands, and much more.

What is WP-CLI?

WP-CLI stands for WordPress Command Line Interface. It is an open source tool that lets you execute WordPress tasks in a command line environment instead of the dashboard pages. It is an alternative to the WordPress admin interface and it lets you manage your WordPress websites remotely.

However, we need to mention that WP-CLI is not a plugin. It is an archived PHP program (.phar) that you can download and set up from your command line. That means you can use the default command terminal of your operating system to utilize WP-CLI.

Get Latest Post Notifications!

Subscribe to our newsletter

Requirements for installing WP-CLI

Now that you know what is WP-CLI, let’s proceed to install it. But before you setup WP-CLI, make sure you have the following on your server:

  • A UNIX based Operating System (OS X, Linux, FreeBSD, Cygwin); limited support for windows
  • WordPress 3.7 or above
  • PHP version 5.6 or above

We know that the majority of us have already met the requirements as these are common requirements. But we thought it was still worth mentioning. So if you have the above mentioned requirements you can proceed to install WP-CLI. However, make sure to backup your WordPress website just in case.

Below we are going to show you how to install WP-CLI for Linux, Mac, and Windows.

How to install WP-CLI on Linux

There are several ways to install WP-CLI. Some of them are using Git, Buildah, Composer, Docker, etc. But it is easier to just download the phar file instead and we will be going with this approach.

You can use WP-CLI on Ubuntu or other Linux distributions. To do that, you have to download the latest version of WP-CLI from the official website. You can download the file manually or you can do the entire process from the command line. Below we are showing the installation from the command line:

Step 1: Download .phar file

Just like you downloaded the phar file on windows, you can download it on Linux in a similar way. Create a folder and put your phar file there. Open your Linux shell (command line) and type in the following command:

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

Step 2: Make the file executable and move to bin folder

Now that you have downloaded the phar file, you have to make it executable in order to use it. To make this file executable, you need to change the file permission. Use the following command to change the file permission:

chmod u+x wp-cli.phar

After that, you should move your file to user/local/bin. This step is optional but we recommend it because, in the Linux directory structure, the bin folder keeps the binaries of the commands you run. You can use the following command to move the file:

sudo mv wp-cli.phar /usr/local/bin/wp

Step 3: Verifying the installation of WP-CLI

Now that you have transformed the archive file into an executable file, it’s time to verify that the installation was successful or not. From your shell, type the following command:

wp --info

If your installation was successful, you will see a similar message like this:

OS:    Linux 4.15.0-32-generic #35-Ubuntu SMP Mon Feb 07 15:45:17 UTC 2022 x86_64
Shell:    /bin/bash
PHP binary:    /opt/sp/php7.3/bin/php
PHP version:    7.3.7
php.ini used:    /etc/php7.3-sp/php.ini
WP-CLI root dir:    phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:    phar://wp-cli.phar/vendor
WP_CLI phar path:    /srv/users/abhi
WP-CLI packages dir:    
WP-CLI global config:    
WP-CLI project config:    
WP-CLI version:    2.2.0

If you see a message that says the command is unknown, that means the installation wasn’t successful. This might have happened if there was something wrong with the download. Repeat the steps from the beginning and if the issue still exists, you can refer to a guide from linoxide.com.

How to install WP-CLI on Mac

WP-CLI can be used on Mac as well and the installation process is identical to Linux. Still, let’s go through the steps for the sake of completion.

Step 1: Download .phar file

Just like you downloaded the phar file on Linux, you have to use the same command on your Mac command terminal:

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

Step 2: Make the file executable and move it to the bin folder

Now that the file is downloaded, you have to make it executable and move it to the bin folder of your Mac directory. You can use the following command to make the file executable

chmod +x wp-cli.phar

To move the file to the bin directory, you can use the command given below:

mv wp-cli.phar /usr/local/bin/wp

Note: The above command moves your file to the bin directory. In addition, it also changes the name of the folder to wp.

Step 3: Verifying the installation of WP-CLI

Now, it’s time to check if WP-CLI was installed properly or not. You need to enter “wp –info” on your command line and check if the installation was successful. If everything went ok you will see a message like this:

PHP binary:/usr/local/php5-5.5.5-20131020-222726/bin/php
neilg@[~]: wp --info
PHP binary:	/usr/bin/php
PHP version:	5.6.30
php.ini used:
WP-CLI root dir:	phar://wp-cli.phar
WP-CLI vendor dir:	phar://wp-cli.phar/vendor
WP_CLI phar path:	/Users/neilg
WP-CLI packages dir:
WP-CLI global config:
WP-CLI project config:
WP-CLI version:	1.4.1

This indicates that your WP-CLI has been installed and it’s ready to be used. If you see a message that the command is unknown, that means the installation was unsuccessful. If that is the case, we suggest removing the phar file from the bin directory completely and repeating the steps.

How to install WP-CLI locally on Windows

Windows has limited support for WP-CLI. We have tried it on our system locally using XAMPP for this demonstration. In addition to installing WP-CLI, you also need to add the environment path for PHP and MySQL.

Other than adding the environment path, the process of installation is pretty much similar. Let’s see how you can install WP-CLI on your Windows locally.

Step 1: Download the latest WP-CLI phar file

You can read how to download the phar file from the official WP-CLI website. We are mentioning it here too. Open your CMD as administrator, and type in the following command:

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

It should look something like this in the terminal:

downloading wp-cli.phar file

The file can be found in C:\Windows\system32\wp-cli.phar

If you would like to download the file manually, you can download it from the wordpress.org handbook. You can also follow the guide shown in wordpress.org.

Step 2: Make an executable file

Now that you have downloaded the phar file, create a new folder in your C drive and name it wp-cli (or any name you choose). Paste the phar file inside that folder. Now create a new file named wp.bat inside the same folder C:\wp-cli and add the following command:

@ECHO OFF
php "c:/wp-cli/wp-cli.phar" %*
putting the phar file and wp.bat file in a folder

Step 3: Add wp-cli to your path

Now we need to add WP-CLI to our Environment Variable path. In order to do that, click on your start menu and find “environment variables”.

accessing the system environment variables

From the system properties, click on the ‘Environment Variables’ button located at the bottom. A new window will open up. From the top select ‘Path’ and click on ‘Edit’. Then click on the ‘New’ button and paste it into your path.

adding the path of wp-cli on environment variable

Now you need to add your XAMPP PHP and MySQL paths to your environment variable. After you are done, click on OK, and close this window.

Step 4: Verifying the installation

Now that you have installed wp-cli, we need to verify if indeed it is installed and ready to use. Open your CMD in administrator mode and type in “wp –info”. Here since we named the batch file wp.bat, we are writing the name wp in the command line. If your installation process was successful, you should see the following message:

verifying wp-cli installation

Now you can use WP-CLI from anywhere on your Windows command line. See how easy it was installing wp cli.

Some WP CLI commands

Now that your WP-CLI is installed and set up, you can use its commands to manage your WordPress website. For starters, let’s find some WP CLI commands. Open your command terminal and type ‘wp’ (whatever name you have given to the batch file inside wp-cli folder) and hit Enter. You will be shown all the commands of WP-CLI.

wp command list

Keep hitting ‘Enter’ on your keyboard to find all the commands of WP-CLI. There are lots of commands. If you want to just terminate this batch job just hit Ctrl+C on your keyboard and press Y and hit ‘Enter’.

Now that we know some WP CLI commands, let’s actually try some of these out and see how the WP-CLI system actually works.

Note: In this post, we’ve used WP-CLI on our Windows localhost. That’s why our following screenshots are taken from Windows. But the commands are same for all OSs.

Install WordPress via the command line

Before installing WordPress, first, we need to download the software. To download the core WordPress files follow the steps given below:

Step 1: Create a new folder and download WordPress files in it

Open your CMD and create a directory using the command below:

mkdir ‘wp-website’ (you can give any name to the folder)
‘cd wp-website’
‘wp core download’
downloading wordpress with wp-cli

Give it some time, and WordPress will be downloaded in your created directory. You will see a message saying that your WordPress download was successful. 

Now a WordPress website needs a database to operate. It also needs a theme and some plugins, let’s learn how you can connect a database and install a theme and plugins using WP-CLI.

Step 2: Creating and connecting our database to our WordPress website

To connect a database, we must first create one. From your command terminal type in the command:

mysql -u root
entering mysql command

Now you can enter MySQL commands on your interface. If you want to learn more about MySQL command line then you can learn from MySQLTutorial.com. But moving on, we want to create a database for our newly created website. To do that, type in the following command:

CREATE DATABASE testdb:

Here we’ve created a database named ‘testdb’. Don’t forget to put the colon at the end of the name of the database. Now type ‘exit’ and hit Enter on your keyboard to exit the database command line. 

Now all that is left is to connect the database that we’ve created with our website. To do that, from your command line type the following command:

wp config create -- dbname=testdb --dbuser=root

This will create a wp-config.php file and you will see a success message. Now we have to start our server. To do that type in the command:

wp server

It will then start your server port. You will see a message like this

[Thu Feb  3 12:26:08 2022] PHP 8.0.12 Development Server (http://localhost:8080) started

Copy the address http://localhost:8080 and paste it in your browser’s URL and you should see the WordPress installation section like this.

wordpress setup screen

But we won’t be using the manual process. We’ll install WordPress via the command line.

Step 3: Setting up our WordPress using WP-CLI

Now that your database is connected to your WordPress website, we can now install the core WordPress. To do this, open your command terminal and type in the command:

wp core install --url=”E:\wp-website” --title=”wp website” admin_user=”root” 
--admin_email=”[email protected]” --admin_password=”12345”

The code above means the following things:

–url = location of where your WordPress website is stored

–title = Title of the website

–admin_user = WordPress admin user name

–admin_emial = Email of admin

–admin_password=”12345”

You might recognize these fields as you had to manually enter these when creating a WordPress website. After you enter these commands you should see a success message in your terminal like the following:

setting up wordpress site

In case you are wondering why our terminal looks different, we switched to Powershell. You can do these tasks from the CMD as well.

Now your WordPress website is fully created with WP-CLI. To check your website, type in the command:

wp server

Copy the URL and paste it into your browser.

wordpress on localhost:8080

If you sometimes want to reinstall your WordPress website, then you can read our post on how to reinstall WordPress.

Installing a theme on WordPress using WP-CLI

Now that we have created a WordPress website using the WP-CLI, we need a theme. Let’s see how you can install a theme to your WordPress website.

Open your command terminal and type in the following command:

wp theme install astra

Here we are installing the theme Astra. You can download any theme that you wish just to make sure the name is correct. To check if your theme has been installed properly or not, you have to access your WordPress files. Go to wp-content > themes and check if Astra has been installed or not.

astra theme installed with wp-cli

Here your theme will be downloaded first, and then installed. It won’t be activated. To activate it, type in the following command:

wp theme activate astra

You will see a success message. Now to check if your theme is installed and activated properly, type the “wp server” to activate your localhost port then open the link as you did before.

astra theme

As you can see that your Astra theme has been installed and activated successfully. It looks blank because none of the plugins were installed. We will learn how to add plugins later in the article.

Deleting a theme

Let’s learn how to delete your theme using a wp-cli command. We’ll show it by deleting the theme Twenty Twenty. To do that, type in the following command in the terminal:

wp theme delete twentytwenty
deleting a theme using wp-cli

Now you can check your themes folder inside wp-content and you will find there is no twentytwenty theme folder. That is how you can remove your theme from your WordPress website using the WP-CLI.

Installing plugins on WordPress using WP-CLI

Now that we have installed and activated a theme for our WordPress website, let’s learn how to add and remove a plugin.

We want to install the Elementor plugin. To add the Elementor plugin use the following command:

wp install plugin Elementor
installing elementor using wp-cli

To activate your plugin, enter the following command:

wp plugin activate Elementor
activating elementor using wp-cli

To install and activate the plugin at the same time, you can use a single command:

wp install plugin Elementor --activate

To check if the plugin is activated, enter: wp plugin list and the terminal will show you the installed plugin

plugin list on command line

As you can see, the Elementor plugin has been installed and activated. Now you can use your WP-CLI to install important security plugins, web builders, or any other plugins.

If you want to deactivate a plugin, type in the following command:

wp plugin deactivate plugin elementor
deactivating elementor using wp-cli

To remove a plugin, for example, we want to remove the ‘hello’ plugin. Type in the following command:

wp plugin uninstall hello

Your plugin will be uninstalled now.

This is how you can manage your WordPress website using the WP-CLI. As you can see there are tons of WP-CLI commands out there. You can edit your posts, add or remove users, manage plugins and themes, and so on. Showing each one of these commands will take some time, so we have just demonstrated some basic commands.

If you ever get stuck or cannot do a certain task, just search online or refer to the official WP-CLI command documentation.

Conclusion

Setting up this command line might be time taking or a difficult process. But once you are done, you will find how convenient it is to manage your WordPress websites from the terminal. But if you are accustomed to managing your WordPress website from the dashboard, we recommend using the dashboard.

Getting used to the WP-CLI might take some time and practice, but it streamlines your workflow. You can manage your posts, users, plugins, themes, etc. with just a few lines of commands. Once you have developed speed, you can manage multiple WordPress websites with ease and efficiency.

We hope that you found this guide helpful. Leave us a comment and let us know your thoughts. Till then, happy coding!


Disclaimer: This post may contain affiliate links and we may receive a small commission if you purchase something by following them. However, we recommend services/products that we believe good to serve your purpose.

Leave a Reply

Your email address will not be published. Required fields are marked *