How to Install and Use WP-CLI to Manage a WordPress Site + Commands and Cheat Sheet
WordPress is one of the most user-friendly content management systems thanks to its intuitive admin interface. Whether you’re a beginner or a developer, managing a WordPress website from the admin panel is easy.
However, there is another way of managing a WordPress site. This method involves using the WordPress command-line interface or WP-CLI. This command-line tool lets website administrators manage websites using simple commands rather than navigating the admin panel.
This article will discuss what WP-CLI is, how to install and access it, and introduce common WP-CLI commands.
What is WP-CLI
WP-CLI is a tool that lets you manage your WordPress site through a command-line interface. You can execute tasks like installing plugins, updating WordPress, and activating themes.
The biggest benefit of WP-CLI is saving time when configuring WordPress websites. For example, you can use a command to install multiple plugins on a WordPress multisite environment at once.
Some WP-CLI commands also let you perform tasks not supported by the WordPress admin panel. For example, you can generate bulk posts and perform database searches and replacements.
The WP-CLI development team regularly updates this command-line tool to fix issues, add new commands, and ensure it conforms to the latest PHP version. The latest version is WP-CLI v2, with the newest release being 2.6.0.
WP-CLI v2 has the following minimum requirements:
- PHP 5.6 or higher.
- WordPress 3.7 or higher.
- UNIX-like environment. Only limited support is available on Windows.
Important! Although you can install WP-CLI on older PHP and WordPress releases, we recommend using the latest versions to prevent security issues.
How to Install WP-CLI
You can install the WP-CLI tool on a virtual private server (VPS) or a local Linux system. But before you proceed, ensure your Linux, PHP, and WordPress environment fulfill the minimum requirements.
Here are the steps for installing WP-CLI. Firstly, download the wp-cli.phar file and install it on your server:
- Download the WP-CLI Phar file using this command:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
- Confirm that it has successfully downloaded the installation package using this command:
php wp-cli.phar --info
- Make the wp-cli.phar file executable using this command:
chmod +x wp-cli.phar
- To finalize the installation, move everything into the final location.
sudo mv wp-cli.phar /usr/local/bin/wp
Once you have installed WP-CLI, you can start working with your WordPress content and files through the command-line tool.
Hostinger’s Premium Shared Hosting plan or higher and the WordPress Starter plan or higher already have the latest version of WP-CLI installed. It’s accessible using the hosting account’s SSH access.
WP-CLI Command Fundamentals
WP-CLI is a straightforward tool if you’re already familiar with the command-line environment. It is often faster than going into the WordPress administration panel and clicking through various options.
By default, WP-CLI comes with numerous built-in commands. You can add more by scripting custom commands or installing WordPress plugins that support WP-CLI. To see the list of tools compatible with WP-CLI, read the official WordPress handbook.
Now, let’s take a look at the fundamental WP-CLI commands:
How to Check WP-CLI Version
Use the following command to check the WP-CLI version quickly:
wp cli version
You should see the following output:
Alternatively, use the following command to see more information about your server and WordPress configuration, including the PHP and MySQL information:
You should see the following output and find the WP-CLI version on the final line.
PHP binary: /opt/alt/php81/usr/bin/php PHP version: 8.1.0 php.ini used: /opt/alt/php81/etc/php.ini MySQL binary: /usr/bin/mysql MySQL version: mysql Ver 15.1 Distrib 10.5.12-MariaDB, for Linux (x86_64) using readline 5.1 SQL modes: 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: /home/website WP-CLI packages dir: WP-CLI global config: WP-CLI project config: WP-CLI version: 2.6.0
Important! The actual output may differ depending on your installation path, PHP, and WP-CLI version.
If you need to update WP-CLI, use the following command:
wp cli update
How to Access the List of WP-CLI Commands and Help Documentation
WP-CLI has built-in documentation that contains basic command information. Enter the following command to open the documentation:
You should see the output as follow:
NAME wp DESCRIPTION Manage WordPress through the command-line. SYNOPSIS wp <command> SUBCOMMANDS cache Adds, removes, fetches, and flushes the WP Object Cache object. cap Adds, removes, and lists capabilities of a user role. cli Reviews current WP-CLI info, checks for updates, or views defined aliases. comment Creates, updates, deletes, and moderates comments. config Generates and reads the wp-config.php file. core Downloads, installs, updates, and manages a WordPress installation. .....
To exit the help page, press the Q key.
Learn More About a Specific Command
You can access a separate help page for each command using the subcommands listed on the wp help output. For example, enter the following to see more details about the comment command:
wp help comment
Here is the output of that command:
NAME wp comment DESCRIPTION Manage comments. SYNOPSIS wp comment <command> SUBCOMMANDS approve Approve a comment. count Count comments, on whole blog or on a given post. create Create a new comment. delete Delete a comment. exists Verify whether a comment exists. generate Generate some number of new dummy comments. .....
You can also see the help page for subcommands. For example, here is the command to see the help page for the count subcommand:
wp help comment count
General WP-CLI Commands
After understanding the basics of WP-CLI, let’s look at general WP-CLI commands to perform everyday WordPress tasks.
How to Install WordPress via WP-CLI
The first step in installing WordPress is creating a MySQL database. If you’re working on a VPS or local VPS machine, ensure MySQL is installed on your Ubuntu or CentOS machine.
With Hostinger, you can make a MySQL database in hPanel via Hosting -> Manage -> Databases -> MySQL Databases.
Alternatively, follow these steps to make a new MySQL database with WP-CLI:
- Log in to MySQL as root to create a new user.
mysql -u root -p
- Add a user and database by entering these lines one by one:
CREATE USER ‘username’; CREATE DATABASE ‘databasename’;
Don’t forget to change the above values to your liking.
- Grant all permission to the user to modify the database.
GRANT ALL PRIVILEGES ON databasename.* TO ‘username’ IDENTIFIED BY ‘yourpassword’;
- Once done, log out from MySQL by typing:
Now, let’s continue installing WordPress. However, you need to be in the public_html directory since it is where your website files should reside. To see the current directory, enter:
In case you are not in public_html, you have to move accordingly:
Important! You must first navigate to the public_html folder to access and manage your website using the WordPress command-line interface.
Once you’re in the public_html directory, follow these steps:
- Download the newest WordPress core version by executing the following command:
wp core download
- Enter the wp config create command to MySQL database credentials to WordPress:
wp config create --dbname=wordpress --dbuser=user --dbpass=password --dbhost=localhost --dbprefix=wp_
Don’t forget to replace the default values with your database details.
- Use the wp core install command to add these details and complete the WordPress installation:
wp core install --url=yourdomain.com --title=Site_Title --admin_user=admin_username --admin_password=admin_password --firstname.lastname@example.org
Remember to replace the default values with your credentials. After the installation is complete, you should see the following output:
Success: WordPress installed successfully.
How to Install and Manage WordPress Themes via WP-CLI
The wp theme command lets you install and manage WordPress themes via WP-CLI.
Let’s start by listing installed themes using this command:
wp theme list
Here is an example of the command’s output:
+------------------+----------+-----------+---------+ | name | status | update | version | +------------------+----------+-----------+---------+ | astra | inactive | available | 3.9.2 | | twentytwenty | inactive | none | 2.0 | | twentytwentyone | inactive | none | 1.6 | | twentytwentytwo | active | none | 1.2 | +------------------+----------+-----------+---------+
From the status, we can see that the active theme is Twenty Twenty-Two. You can change the active theme using the wp theme activate command followed by the theme’s name.
For example, use the following command to change the active theme to Astra.
wp theme activate astra
Here is the output of the command:
Success: Switched to 'Astra' theme.
You can also utilize WP-CLI to search for new themes in the WordPress theme directory. In this example, we’d like to search for themes with Bootstrap support:
wp theme search bootstrap
You should see an output similar to the following:
Success: Showing 10 of 630 themes.
+-----------------------+-----------------------+--------+ | name | slug | rating | +-----------------------+-----------------------+--------+ | Bootstrap Fitness | bootstrap-fitness | 0 | | Bootstrap Coach | bootstrap-coach | 0 | | The Bootstrap Blog | the-bootstrap-blog | 100 | | Bootstrap Photography | bootstrap-photography | 0 | | Bootstrap Blog | bootstrap-blog | 40 | | BPT Bootstrap | bpt-bootstrap | 74 | | Bootstrap Basic4 | bootstrap-basic4 | 90 | | Flat Bootstrap | flat-bootstrap | 100 | | Bootstrap Basic | bootstrap-basic | 100 | | Arvada | arvada | 0 | +-----------------------+-----------------------+--------+
Although there’s no visual aid to help you choose a theme, you can refer to the available rating list. For instance, The Bootstrap Blog theme from the list has a rating of 100. To install and activate it, use this command:
wp theme install the-bootstrap-blog --activate
If you want to see more wp theme subcommands, just run the command without any subcommands. You should see the following output:
usage: wp theme activate <theme> or: wp theme auto-updates <command> or: wp theme delete [<theme>...] [--all] [--force] or: wp theme disable <theme> [--network] or: wp theme enable <theme> [--network] [--activate] .....
How to Install and Manage WordPress Plugins via WP-CLI
You can install and manage WordPress plugins through the command-line. To list all currently installed plugins, enter:
wp plugin list
Here is an example of the output:
+----------------------+----------+-----------+---------+ | name | status | update | version | +----------------------+----------+-----------+---------+ | all-in-one-seo-pack | active | none | 4.2.4 | | create-block-theme | inactive | none | 1.1.3 | | gutenberg | active | available | 14.0.2 | | hello | inactive | none | 1.7.2 | | wp-reset | active | none | 1.95 | +----------------------+----------+-----------+---------+
To activate any inactive plugin, use the wp plugin activate command followed by the plugin’s name. For example:
wp plugin activate create-block-theme
To add more plugins, search the WordPress plugin directory using the wp plugin search command. For example, use the following command to find WordPress cache plugins:
wp plugin search cache
Expect a similar output as this:
Success: Showing 10 of 1010 plugins. +--------------------------------+--------------------------+--------+ | name | slug | rating | +--------------------------------+--------------------------+--------+ | LiteSpeed Cache | litespeed-cache | 96 | | W3 Total Cache | w3-total-cache | 88 | | WP Fastest Cache | wp-fastest-cache | 98 | | WP Super Cache | wp-super-cache | 86 | | Redis Object Cache | redis-cache | 94 | | SiteGround Optimizer | sg-cachepress | 86 | | Autoptimize | autoptimize | 94 | +--------------------------------+--------------------------+--------+
Let’s say you want to install and activate the LiteSpeed Cache WordPress plugin from the list above. Use the wp plugin install command followed by the plugin’s name and –activate option.
wp plugin install litespeed-cache --activate
You can also delete plugins using the wp plugin delete command. For example:
wp plugin delete litespeed-cache
Just like with themes, you can run the wp plugin command on its own to see the list of available subcommands.
How to Update WordPress via WP-CLI
Updating WordPress with WP-CLI is a two-step process, as you need to update WordPress files and the database.
- Start by updating the WordPress files:
wp core update
Once the update is complete, you’ll see this output:
Success: WordPress updated successfully.
- Run the command below to check if it is necessary to update the database to the required version:
wp core update-db
- Once the update is complete, check your WordPress version using this command:
wp core version
You can also update installed WordPress themes and plugins.
Run this command to update themes:
wp theme update --all
Similarly, run this command to update plugins:
wp plugin update --all
Note that the –all parameter is used to update all installed themes or plugins. You can change it to a specific theme or plugin by replacing –all with the theme or plugin name.
How to Change WordPress URL via WP-CLI
WordPress lets you change the home and site URLs through the admin dashboard. That said, you can change these settings using the wp option update command.
To change the home address, run the following command:
wp option update home ‘http://example.com’
You should see this output:
Success: Updated 'home' option.
You can change the site URL using the same command. All you have to do is replace the home option with site URL.
wp option update siteurl ‘http://example.com’
You should see this output:
Success: Updated 'siteurl' option.
How to Reinstall WordPress Core via WP-CLI
You can use the same WordPress installation command to reinstall the core software.
However, you will need to use the –skip-content option to download WordPress without the default themes and plugins and the –force option to overwrite the existing files.
With these options, the new WordPress core will replace the current installation, but the existing themes and plugins will remain:
wp core download --skip-content --force
Once the installation is complete, you should see the following output:
Success: WordPress downloaded.
How to Manage Content via WP-CLI
WP-CLI lets you manage content through the command-line. Although it’s not the most convenient content management method, it can be useful for tasks you can’t do on the WordPress admin panel, such as creating bulk posts.
WP-CLI provides several ways to manage content through the command-line. Although it may not be very comfortable to write posts on the terminal, let’s learn how to do it.
To see the list of all posts, use the following command:
wp post list
Here is an example of the output:
+----+--------------+-------------+---------------------+-------------+ | ID | post_title | post_name | post_date | post_status | +----+--------------+-------------+---------------------+-------------+ | 1 | Hello world! | hello-world | 2022-09-08 03:39:33 | publish | +----+--------------+-------------+---------------------+-------------+
In the example above, Hello world!’s post ID is 1. We can delete the post with this command:
wp post delete 1
Here is the output:
Success: Trashed post 1.
To create a new post, use this command:
wp post create --post_status=publish --post_title="This Post Was Created With WP-CLI" --edit
This command will open the Vim text editor. Input the content on the text editor, then exit Vim by typing :wq and pressing Enter. You should see the following output containing the post ID:
Success: Created post 10.
If you have a content draft in the TXT file format within the directory, you can create a new post by importing directly from that file using this command:
wp post create ./post.txt --post_title='Sample Post' --post_status=publish
Lastly, you can generate multiple posts in WP-CLI using a single command. This feature is useful to test how your WordPress site will work with many posts. To generate 10 posts, enter this command:
wp post generate --count=10
You can automate the image import process with WP-CLI. For example, use the following command to import all images from the images_for_site folder:
wp media import images-for-site/*
You will see this output:
Imported file 'images-for-site/image-1.jpg' as attachment ID 25. Imported file 'images-for-site/image-2.jpg' as attachment ID 26. Success: Imported 2 of 2 items.
How to Export/Import WordPress via WP-CLI
You can easily export or import content from one WordPress installation to another using WP-CLI.
To export posts, terms, authors, comments, and attachments to an XML file, enter the command below:
The XML file should now be available in your public_html folder. Usually, it’s named yourwebsitename.wordpress.2022-06-06.000.xml, with the date on the file name representing the export date.
To import the XML file to a WordPress installation, install and activate the WordPress Importer plugin using this command:
wp plugin install wordpress-importer --activate
Once the plugin installation is complete, proceed with the import. Create a new author by using the –authors parameter:
wp import hostingertutorials.wordpress.2020-03-06.000.xml --authors=create
Here is an example of the output:
Success: Finished importing from 'website.wordpress.2022-09-13.000.xml' file. <p>All done. <a href="http://yourdomain.com/wp-admin/">Have fun!</a></p><p>Remember to update the passwords and roles of imported users.</p>
How to Manage Database Using WP-CLI
WP-CLI allows you to run normal SQL queries with the wp db query command. For instance, to see all registered WordPress users, enter:
wp db query "SELECT user_login,ID FROM wp_users;"
Here is an example of the output:
+------------+----+ | user_login | ID | +------------+----+ | user | 1 | +------------+----+
You can run other useful operations like exports. This command will generate an SQL dump file of your WordPress database:
wp db export
Expect an output similar to:
Success: Exported to 'user_wordpress-2022-09-13-3c44b24.sql'.
To import an SQL file into a WordPress database, use this command:
wp db import filename.sql
Here is an example of the output:
Success: Imported from 'filename.sql'.
How to Search and Replace Using WP-CLI
Sometimes, you have to replace some strings in the database, such as the site URLs, after migrating a WordPress site. While an SQL query or a database plugin can do this task, WP-CLI also lets you perform it with the WP-CLI search-replace command.
We recommend making a dry run before the actual search and replace using the –dry-run parameter. This way, you will know how many replacements need to be made.
For example, you may have changed the website’s domain from website.net to website.com and want to replace the URLs in the database. Use this command to perform a dry run for the search and replace:
wp search-replace --dry-run 'website.net' 'website.com'
The output will display the database table list and the replacements needed to be made:
+------------------+-----------------------+--------------+------+ | Table | Column | Replacements | Type | +------------------+-----------------------+--------------+------+ | wp_options | option_name | 0 | SQL | | wp_options | option_value | 2 | PHP | | wp_options | autoload | 0 | SQL | | wp_postmeta | meta_key | 0 | SQL | | wp_postmeta | meta_value | 0 | PHP | | wp_posts | post_content | 3 | SQL | | wp_posts | post_title | 0 | SQL | | wp_posts | post_excerpt | 0 | SQL | | wp_posts | post_status | 0 | SQL | | wp_posts | post_content_filtered | 0 | SQL | | wp_posts | guid | 22 | SQL | +------------------+-----------------------+--------------+------+ Success: 27 replacements to be made.
To perform the actual search and replace using WP-CLI, remove the –dry-run parameter.
wp search-replace 'website.net' 'website.com'
The output will display a similar database table list as the dry run but have the following message at the end:
Success: Made 14 replacements.
The WordPress command-line interface or WP-CLI lets you manage WordPress sites using a command-line interface. Managing WordPress using command-line tools can be more time-efficient as you don’t have to navigate through the dashboard just to do one task.
Moreover, WP-CLI’s ability to complete multiple tasks using a single command and perform functions unavailable in WordPress admin makes it a great tool to streamline your workflow and improve productivity.
We hope this article helps you understand the basics of WP-CLI. To explore more commands, download our WP-CLI cheat sheet or browse the official WP-CLI documentation.
Frequently Asked Questions About WP-CLI
Now that we have discussed how to use WP-CLI, we will answer some common questions about this WordPress tool.
Can I Use WP-CLI Remotely?
es, you can use the WP-CLI tool remotely.
All you have to do is ensure that WP-CLI is installed on your server and enable SSH access. Then, use any SSH client or terminal on your computer to connect to your server using the SSH credentials. Finally, access WP-CLI to configure your WordPress website.
Can I Use WP-CLI With Bash Scripts?
Yes, all you have to do is create the bash script file inside the public_html directory and write the WordPress commands in the file. Run the bash file to execute all the written commands together. This method is useful for consolidating all WP-CLI commands in a single file and running them all using a bash command.