WordPress is one of the most popular CMSs for creating and managing websites. While WordPress has a user-friendly graphical user interface (GUI) for managing content and settings, it also provides a powerful Command-Line Interface (CLI) called WP-CLI.
WP-CLI is a set of command-line tools for managing WordPress installations that can be used in your computer’s Terminal or command prompt. It allows you to perform various tasks such as updating WordPress, plugins, and themes, managing users and roles, and exporting and importing data without leaving the Terminal.
In this article, I’ll tell you how to install and use WP-CLI to manage a WordPress website from the Terminal, including basic and advanced WP-CLI commands for different tasks. So let’s get started!
An Overview of WP-CLI
WP-CLI is an open-source project that is actively developed and maintained by a community of developers. It is compatible with most UNIX-like operating systems, including Linux, macOS, and Windows. WP-CLI requires PHP 5.6.0 or later and can be installed as a global package or a local dependency using the Composer package manager.
One of the main advantages of using WP-CLI is its speed and efficiency. Using command-line tools is often faster than navigating through a GUI, especially when performing repetitive tasks or automating complex operations. WP-CLI commands can be easily scripted and integrated with other command-line tools, making them powerful for developers.
💡 Quick info: Cloudways is a premium sponsor of WP-CLI to help and maintain this open-source project.
Try our WP-CLI Friendly Platform for a Seamless WordPress Control Experience!
Try Cloudways Managed WordPress hosting for built-in WP-CLI and quickly manage your WordPress tasks.
Advantages of WP-CLI
Here are some advantages of using WP-CLI for WordPress.
1. Speed and Efficiency
WP-CLI allows you to perform a wide range of actions on your WordPress site quickly and efficiently, without the need for manual intervention. This can be particularly useful for managing large or complex sites and automating routine tasks.
2. Improved Productivity
With WP-CLI, you can perform multiple tasks simultaneously, saving time and increasing productivity. This can be particularly useful for developers working on multiple sites simultaneously.
3. Enhanced Control
WP-CLI gives you greater control over your WordPress site, allowing you to manage files, plugins, themes, and other components more effectively. This can be particularly useful for developers who need to debug or troubleshoot issues on their site.
4. Improved Security
WP-CLI allows you to manage user accounts, passwords, and other security-related aspects of your site more easily, which can help you to maintain a more secure and stable WordPress installation.
5. Task Automation
Requirements of WP-CLI
To install WP-CLI, you need a hosting solution that offers SSH access.
Many shared hosting providers usually do not offer SSH access. However, almost all popular WordPress hosting offers SSH access to your server. Please make sure your environment meets the following minimum requirements before installing WP-CLI.
- PHP: WP-CLI requires PHP 5.6.0 or later. PHP 7 or later is recommended.
- WordPress: WP-CLI is designed to work with WordPress 3.7 or later.
- Operating System: WP-CLI is compatible with Linux, macOS, and Windows OS. However, WP-CLI offers limited support in the Windows environment.
These are the basic requirements to install and use WP-CLI. The specific requirements may vary depending on the plugins and themes used on your WordPress site.
💡 Note: I’ll use Cloudways’ managed hosting Platform for running WP-CLI commands.
How to Install WP-CLI
To install WP-CLI, you can follow these simple steps:
- Step 1: Connect to the Root Directory via SSH
- Step 2: Download WP-CLI on Your Server
Step 1: Connect to the Root Directory via SSH
To use SSH access to your server, you need credentials (admin username, password), server IP, and port. Next, you must create the key pair. Follow the link below for different operating systems:
- For Windows, PuTTY;
- For Linux, Ubuntu;
- For Mac, Termius.
For this article, I use PuTTY. However, you can use any other terminal emulator of your choice.
- In PuTTY, go to Sessions;
- Enter the Host Name (or IP address) and Port;
- Select SSH for Connection Type;
- Click the Open button at the end.
In the next step, the process will prompt you to enter your username and password, which you can find in the Server Details.
Step 2: Download WP-CLI on Your Server
If you are a Cloudways user, you can skip this step and directly jump to the WP-CLI management part. Because Cloudways servers come with preinstalled and preconfigured WP-CLI.
If you are on any VPS or using a different hosting, run the following command to download the WP-CLI:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
php wp-cli.phar --info
How to Use WP-CLI
Once you have installed WP-CLI on your computer, you can open a Terminal window and run different WP-CLI commands by typing “wp” followed by the command and its arguments.
- Manage WordPress via WP-CLI
- Manage WordPress Theme via WP-CLI
- Manage WordPress Plugin via WP-CLI
- Manage WordPress Core via WP-CLI
- Manage WordPress Multisite via WP-CLI
- Search or Replace Strings via WP-CLI
- List All Supported Commands of WP-CLI
1. Manage WordPress via WP-CLI
The next step involves downloading and configuring WordPress through WP-CLI commands.
The following command will download the latest version of the WordPress core files.
wp core download
Create wp-config File
Use the following command to create the wp-config.php file that contains the main configuration settings for the WordPress website.
wp config create --dbname=testing --dbuser=wp --dbpass=securepswd --locale=ro_RO
For this article, I have used the following database details:
- Database Name: testing
- Database Username: wp
- Database Password: securepswd
Examples of the Command Options
The above-mentioned command is just one of the variations that highlight the power of WP-CLI. As you can guess, this generates the standard version of the wp-config file.
The following variation will set up the wp-config file with WP_DEBUG and WP_DEBUG_LOG.
# Enable WP_DEBUG and WP_DEBUG_LOG wp config create --dbname=testing --dbuser=wp --dbpass=securepswd --extra-php <<PHP define( 'WP_DEBUG', true ); define( 'WP_DEBUG_LOG', true ); PHP #Success: Generated 'wp-config.php' file.
There is always a chance that the admin user credentials can get stored in the bash shell history. This can become a serious security breach. WP-CLI provides a version of the wp-config command to deal with the issue.
# Avoid disclosing password to bash history by reading from password.txt # Using --prompt=dbpass will prompt for the 'dbpass' argument $ wp config create --dbname=testing --dbuser=wp --prompt=dbpass < password.txt #Success: Generated 'wp-config.php' file.
Use the following command to create the database. Note that this command will create a fresh database with the name used in the wp-config file.
wp db create
It is easy to install WordPress from the command line using WP-CLI on your server. The command requires parameters including URL, Title, Admin Username, Password, and Admin Email.
wp core install --url=your_domain --title=Your_Blog_Title --admin_user=username --admin_password=password --admin_email=your_email.com
Congratulations! Your WordPress site has been set up successfully.
2. Manage WordPress Theme via WP-CLI
Using WP-CLI, you can connect the server directly to the WordPress theme repository and import the theme quickly. Installing and activating WordPress via WP-CLI processes are simple. You can also update and delete themes through WP-CLI as well.
The command wp theme combines options and switches to handle all theme management tasks.
Use the following command to install a theme. For example, Twenty Twenty Two,
wp theme install twentytwentytwo
💡 Note: Make sure you use the theme’s name as it appears in the page URL. For instance, as you can see in the following screenshot, I have used twentytwentytwo as an argument for the command.
The system will generate a notification about the installation in progress. To activate the theme, run the following command.
wp theme activate twentytwentytwo
Your WordPress website’s theme will be changed to Twenty Twenty Two.
If you want to learn more about the SSH terminal, check out this article on handling files and folders with an SSH terminal.
3. Manage WordPress Plugin via WP-CLI
Installing WordPress plugins via WP-CLI is almost instantaneous as no time is wasted clicking buttons. Also, you might not even have to log in to the WordPress admin panel to install the plugins.
To install a WordPress plugin, run the following command.
wp plugin install [plugin name]
💡 Note: Replace [plugin name] with the name of the plugins as it appears in the page URL. For example, to install Contact Form 7, use contact-form-7 as shown in the following image.
Let’s also install WooCommerce, a popular plugin for creating ecommerce stores on WordPress. Start by typing the following command.
wp plugin install woocommerce
It takes just a few moments to install the plugin, and to activate it, run the following command.
wp plugin activate woocommerce
4. Manage WordPress Core via WP-CLI
To check the version of the WordPress Core, run the following command.
wp core version
That will return the version of the WordPress Core. To update the Core files, run the following command.
wp core update
The WordPress Core will be updated to the latest available version on WordPress.org.
5. Manage WordPress Multisite via WP-CLI
If you are a web agency with many clients, WP-CLI is the quickest method for updating all the sites on WordPress Multisite.
To run WP-CLI commands on WordPress Multisite, you must mention the website URL in the network using the –url switch.
wp theme status twentytwentytwo
6. Search or Replace Strings via WP-CLI
Most people face issues when migrating their site to a new host. If you want to replace your old URL with a new one, then the search and replace command would help.
Use the following WP-CLI command for search & replace:
wp search-replace <‘old’> <‘new’>
wp search-replace https://oldsite.com https://newsite.com
7. List All Supported Commands of WP-CLI
To start using WP-CLI, type wp and hit Enter. The system will list all the supported commands for managing WordPress via WP-CLI.
Run the following command to get more details about the command and its options.
wp help <command name>
For example, run the following command to get more details about the plugin commands.
wp help plugin
List of WP-CLI Commands
Here’s a list of commonly used WP-CLI commands.
|Details about command and options
|wp cli version
|To check the WP CLI version
|php wp-cli.phar –info
|Making file executable
|wp core download
|Download the latest version of the WordPress core files
|wp config create
|Create config file
|wp db create
|Create a new DB
|wp theme install
|wp theme activate
|wp theme deactivate
|wp plugin install
|wp plugin activate
|Searches/replaces strings in DB
|wp help plugin
|Details about the Plugin command
There are many more WP-CLI commands available depending on what you want to accomplish with your WordPress site. The full list of WP-CLI commands and their usage instructions are in the official documentation.
Use WP-CLI on Cloudways
Cloudways Platform has integrated WP-CLI by default in the SSH terminal. To use WP-CLI on Cloudways, please follow the instructions.
- Sign up or log in to your Cloudways account;
- Enter Application & Server Details and choose WordPress as your application;
- Select the server specifications, including the cloud provider, server size, and location;
- Click on the Launch Now button to create a new server with WordPress site.
- Click on your newly launched server to proceed to the next screen.
- Go to Server Management → Master Credentials and Launch SSH Terminal.
- A new terminal window opens on your screen.
From Server Management, you can find the Master Credentials for logging into the terminal. The easiest way to log in is to copy and paste the credentials into the space provided. Right-click on the system to paste the username and password.
If you use multiple WordPress sites, you should select the site you want to work with via WP-CLI.
Type ls command and find out where you are in the directory structure.
See the subdirectories in the applications folder running the following command.
Since I want to go to the Applications folder, I will run the following command:
As you can see, I have several applications installed on the server. I will select the application I want to update.
To do this, type the cd command with the name of the target application.
cd [application name]
Go to the public_html directory by running the following command.
That’s it, now you can easily manage your WordPress application through WP-CLI.
With a wide range of commands available, WP-CLI makes it easy to install, activate, deactivate, update, and manage WordPress plugins, themes, and core files. It can also be used to manage posts, users, and the WordPress database, making it a powerful tool for WordPress management.
Whether you’re managing a single site or multiple sites, WP-CLI can streamline your workflow and save you time and effort. You can become a more efficient WordPress site manager and developer by mastering the various WP-CLI commands.
Frequently Asked Questions
Q. How to run WP CLI commands on WordPress?
Follow the below steps to run the WP CLI commands.
- Connect to the Root Directory via SSH;
- Download WP-CLI on Your Server;
- Install WordPress and start using WP CLI.
Q. In which directory can we install WordPress?
It would be great if you installed WordPress in your root directory. On Cloudways, we install it on the public_html folder.
Q. How to see users in WP-CLI?
Here’s the command to see the users in WP-CLI.
wp user list --field=ID
Q. How can you directly activate and deactivate plugins on WP-CLI?
You can install a plugin with the “wp plugin install” command.
wp plugin install woocommerce
You can activate a plugin with “wp plugin activate” command.
wp plugin activate woocommerce
You can deactivate a plugin with “wp plugin deactivate” command.
wp plugin deactivate woocommerce
Farhan is a community manager at Cloudways. He loves to work with WordPress and has a passion for web development. Mostly, he spends his time interacting with the people in the WordPress community. Apart from his work life, Farhan spends his time gaming and playing sports. Feel free to contact him at [email protected]