How to Install and Use WP-CLI to Manage a WordPress Blog
In this article, you are going to learn about WP-CLI (WordPress Command Line Interface). It is a powerful WordPress tool that will improve your productivity and web management skills.
But before we show you how to install and use it, let’s briefly uncover what WP-CLI is.
What is WP-CLI?
WP-CLI is a tool that gives you the ability to manage your WordPress site through a command-line interface. You can execute standard functions, such as plugin installation, post creation, WordPress updates, and so on.
Also, it allows you to perform commands that are not supported through the standard WordPress back-end.
The biggest benefit to WP-CLI is that it can save you a lot of time when you’re configuring, maintaining, or installing WordPress websites. If you have multiple sites, you no longer need to log in to each account to take care of basic tasks. What’s great, the WP-CLI commands can be automated, as well.
Sounds interesting, right? So without further ado, here are the steps to install and use WP-CLI.
With Hostinger’s shared hosting plans, WP-CLI is already installed. Therefore, you only need to connect to your account via SSH in order to use it. Keep in mind, however, that our Single Shared Hosting plan excludes this functionality. If you want to install the feature on a VPS or a local Linux system, you can follow the steps below:
- Download the WP-CLI tool from Github with the following command:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
- Next, confirm that it has been downloaded successfully:
php wp-cli.phar --info
- Now you need to set wp-cli.phar to be an executable file. Simply enter:
chmod +x wp-cli.phar
- To finalize the installation, move everything into the final location:
sudo mv wp-cli.phar /usr/local/bin/wp
Now that WP-CLI is installed, you can start working with your WordPress content and files through the command line.
Understanding WP-CLI Commands
WP-CLI is a straightforward tool if you’re already familiar with the command-line environment. Surprisingly, 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. Thankfully, you can extend them by scripting your own custom commands or installing plugins that support WP-CLI. If you want to see the list of compatible tools, read this WordPress handbook.
Now, putting that aside, let’s take a look at a few basic WP-CLI commands:
Checking WP-CLI Version
Simply type this command to check the WP-CLI version information:
The output should look like this:
PHP binary: /usr/bin/php PHP version: 7.3.11 php.ini used: /opt/alt/php70/etc/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: /Users/test WP-CLI packages dir: WP-CLI global config: WP-CLI project config: WP-CLI version: 2.4.0
Accessing The List of WP-CLI Commands and Help Documentation
Type the following line if you want to see other commands that you can use with WP-CLI:
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 Manage WP-CLI itself. comment Creates, updates, deletes, and moderates comments. core Download, installs, updates and manages a WordPress installation. cron Tests, runs, and deletesq WP-Cron events and schedules. db Perform basic database operations using credentials stored in wp-config.php eval Execute arbitrary PHP code. eval-file Load and execute a PHP file. .....
To exit the help page, press the Q button on your keyboard.
Learning More About a Specific Command
You can also access a separate help page for each command. For example, this is how you see more details about the comment command:
wp help comment
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. .....
As you can see from the output, each WP-CLI command has a list of subcommands. Let’s say we want to get the number of available comments. We can use the following argument:
wp comment count
What’s cool, you can go even further to see the help page for a subcommand of a subcommand, like this:
wp help comment count
NAME wp comment count DESCRIPTION Count comments, on whole blog or on a given post. SYNOPSIS wp comment count [<post-id>] OPTIONS [<post-id>] The ID of the post to count comments in. .....
WP-CLI Commands Cheat Sheet
If it’s the first time you are using WP-CLI, this cheat sheet will be very useful. It has all official commands together with examples and global parameters.
Using WP-CLI to Install WordPress
It is possible to install WordPress through WP-CLI. However, you will need to create a MySQL database for your blog before proceeding. When using Hostinger, you can create the MySQL database in Control Panel -> MySQL Databases section.
- First, we will log in to MySQL as root to create a new user.
mysql -u root -p
Then, we can add a user and database by entering these lines one by one:
CREATE USER username; CREATE DATABASE databasename;
Don’t forget to change the values to your liking.
- After that, we have to grant all permission to the user to modify the database.
GRANT ALL PRIVILEGES ON databasename.* TO ‘username’ IDENTIFIED BY ‘yourpassword’;
- Once it’s done, log out from MySQL by typing:
Now you can continue with the WordPress installation. However, you need to be in the public_html directory since it is usually 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:
- If you are already on that directory, you can download the newest WordPress version by executing:
wp core download
- Now that the download is complete, visit your website to check the installation. You will see a standard WordPress installation form. Although you can fill in all the required columns using a web browser, it would be easier to do that with WP-CLI.
- We will use the wp core command in order to add our MySQL database credentials to WordPress:
wp core config --dbname=wordpress --dbuser=user --dbpass=password --dbhost=localhost --dbprefix=wp_
Replace the default values with your own database details:
- dbname – the MySQL database name.
- dbuser – the database username.
- dbpass – your MySQL user password.
- dbhost – the MySQL server hostname.
- dbprefix – MySQL database table prefix. You can leave it as wp_.
- Now if you go to your WordPress site again, you will notice that it asks to enter a few additional details.
We will use the same wp core command in order to add these details and complete the WordPress installation:
wp core install --url=yourdomain.com --title=Site_Title --admiwp admin_user=admin_username --admin_password=admin_password --firstname.lastname@example.org
The output should be:
Success: WordPress installed successfully.
Good job! You have completed WordPress installation through WP-CLI.
An important thing to note, if you want to access and manage your WordPress blog using the command-line interface, you have to navigate to the public_html folder first.
Using WP-CLI to Install and Manage WordPress Themes
Let’s try to install a theme by using WP-XLI.
- Start by listing the already installed themes:
wp theme list
+-----------------+----------+--------+---------+ | name | status | update | version | +-----------------+----------+--------+---------+ | twentynineteen | active | none | 1.4 | | twentyseventeen | inactive | none | 2.2 | | twentysixteen | inactive | none | 2.0 | +-----------------+----------+--------+---------+
- From the status, we can see that the active theme is twentynineteen. Let’s say we want to replace it with twentyseventeen.
wp theme activate twentyseventeen
The output should be:
Success: Switched to 'Twenty Seventeen' theme.
- You can even use WP-CLI to search for new themes in the official WordPress theme directory. In this example, we’d like to search for a theme with bootstrap support:
wp theme search bootstrap
Success: Showing 10 of 292 themes.
+----------------------+----------------------+--------+ | name | slug | rating | +----------------------+----------------------+--------+ | Bootstrap Lightpress | bootstrap-lightpress | 0 | | Bootstrap News | bootstrap-news | 0 | | Bootstrap Photo | bootstrap-photo | 0 | | Bootstrap Beauty | bootstrap-beauty | 100 | | Bootstrap Journal | bootstrap-journal | 0 | | Bootstrap Blog | bootstrap-blog | 40 | | devdmbootstrap4 | devdmbootstrap4 | 100 | | WP Bootstrap 4 | wp-bootstrap-4 | 84 | | BPT Bootstrap | bpt-bootstrap | 74 | | BootstrapFast | bootstrapfast | 0 | +----------------------+----------------------+--------+
- We know you can’t say much about a theme from only its name. That being said, WP Bootstrap 4 sounds interesting and has a rating of 84. In order to install and activate the theme with WP-CLI, use:
wp theme install WP-Bootstrap-4 --activate
Using WP-CLI to Install and Manage WordPress Plugins
Next, we’ll take a look at plugin management using WP-CLI.
- Just like themes, you can install and manage plugins through the command line. In order to list the already installed plugins, enter:
wp plugin list
+---------+----------+--------+---------+ | name | status | update | version | +---------+----------+--------+---------+ | akismet | inactive | none | 4.1.2 | | hello | inactive | none | 1.7.2 | +---------+----------+--------+---------+
- As you might notice, we don’t have many plugins. Let’s install a contact form plugin for our WordPress blog. First, search the plugin directory for the contact form 7 plugin:
wp plugin search "contact form 7"
Success: Showing 10 of 2514 plugins. +--------------------------------------+---------------------------+--------+ | name | slug | rating | +--------------------------------------+---------------------------+--------+ | Contact Form 7 | contact-form-7 | 92 | | Contact Bank - Contact Forms Builder | contact-bank | 86 | | Contact Form | contact-forms-builder | 90 | | Contact Form | contact-form-ready | 96 | | Contact Form | contact-form-add | 76 | | Contact Form | powr-contact-form | 100 | | Contact Form | better-contact-form | 100 | | Contact Form | contact-form-master | 96 | | Contact Form | contact-form-maker | 88 | | Contact Form by Supsystic | contact-form-by-supsystic | 92 | +--------------------------------------+---------------------------+--------+
- Contact Form 7 is the first one on the list. We want to install and activate it since it’s a great plugin for creating lightweight contact forms:
wp plugin install contact-form-7 --activate
Same as with themes, plugins can be installed from .zip archive and a URL.
- In order to delete a plugin change install to delete:
wp plugin delete contact-form-7
Deleted 'contact-form-7' plugin. Success: Deleted 1 of 1 plugins.
Using WP-CLI to Update WordPress
Updating WordPress with WP-CLI is a two-step process, as you need to update WordPress files and the database in order to complete this process.
- Start by updating the WordPress core files:
wp core update
- Run the command below to check and if it is necessary to update the database to a required version:
wp core update-db
- This is how to update WordPress themes and plugins with the command line. Note that the –all parameter is used to update all installed themes/plugins, but you can change it to a specific theme/plugin by replacing –all with the item’s name.
- For themes:
wp theme update --all
- For plugins:
wp plugin update --all
- For themes:
Using WP-CLI to Manage Content
In this section, you will find how to use WP-CLI to manage your post and media.
WP-CLI provides several ways to manage content through the command line. It may not be very comfortable to write posts in the terminal. However, for the sake of learning, let’s see how to do it.
- To see the list of all posts use:
wp post list
+----+--------------+-------------+---------------------+-------------+ | ID | post_title | post_name | post_date | post_status | +----+--------------+-------------+---------------------+-------------+ | 1 | Hello world! | hello-world | 2020-03-06 12:22:55 | publish | +----+--------------+-------------+---------------------+-------------+
- As you can see we only have one post (Hello world!) and the ID of this post is 1. We can delete it with:
wp post delete 1
Success: Trashed post 1.
- To create a new post, use:
wp post create --post_status=publish --post_title="This Post Was Created With WP-CLI" --edit
- It’s also possible to create a new post and import post content directly from a .txt file:
wp post create ./post.txt --post_title='Sample Post' --post_status=publish
- Apart from creating posts, you can also generate them in WP-CLI. This feature is useful if you want to test how your WordPress site will work with a lot of posts. To autogenerate 10 posts, enter this following line:
wp post generate --count=10
With WP-CLI, you can automate the image import process. For this example, we want to import all images from the images_for_site folder. We can achieve it by using a single command:
wp media import images_for_site/*
Using WP-CLI To Export/Import WordPress
Thanks to WP-CLI, you can easily export or import content from one WordPress installation to another.
- In order to export posts, terms, authors, comments, and attachments to an XML file, enter the command below:
- Let’s say we want to import the same XML file to another WordPress instance. First of all, we need to install the wp-importer plugin:
wp plugin install wordpress-importer --activate
- Once the installation is complete, we can proceed with the import. We will create a new author by using the –authors parameter:
wp import hostingertutorials.wordpress.2020-03-06.000.xml --authors=create
<p>All done. <a href="http://hostinger-dev-17.xyz/wp-admin/">Have fun!</a></p><p>Remember to update the passwords and roles of imported users.</p> Success: Finished importing from 'hostingerdev.wordpress.2020-03-06.000.xml' file.
Using WP-CLI to Manage Database
WP-CLI also allows you to manage your database. Here are some basics:
- The tool allows you to run normal SQL queries with the wp db query command. For instance, in order to see all registered WordPress users, enter:
wp db query "SELECT user_login,ID FROM wp_users;"
+------------+----+ | user_login | ID | +------------+----+ | user | 1 | +------------+----+
- You can run other useful operations like exports. It will generate an SQL dump file of your WordPress database:
wp db export
Success: Exported to 'u373726772_wordpress-2020-03-06-3c44b24.sql'.
- To import an SQL file into a WordPress database, use:
wp db import filename.sql
Success: Imported from 'filename.sql'.
Using WP-CLI to Search and Replace
Have you ever moved WordPress from a local development or staging to hosting? If you have, you know how useful search and replace can be. While an SQL query or a plugin might do it, you can easily perform search and replace in WP-CLI with one command.
Let’s say we have changed our domain name from hostinger-dev-17.xyz to hostinger-dev-17.net and we need to update all URLs in our WordPress database.
- It’s always recommended to make a ‘dry run’ before performing the actual search and replace. It will show how many instances would be changed:
wp search-replace --dry-run 'hostinger-dev-17.xyz' 'hostinger-dev-17.net'
Success: 14 replacements to be made.
- Now, in order to perform the actual search and replace with WP-CLI, remove the –dry-run parameter:
wp search-replace 'hostinger-dev-17.xyz' 'hostinger-dev-17.net'
Success: Made 14 replacements.
WP-CLI makes it possible for you to manage your WordPress site through a command-line interface. As you have seen, it can prove to be more efficient than performing tasks from your WordPress admin page.
What’s great, this tool is extendable through third-party programs and advanced commands. All in all, it’s a great tool to streamline your workflow and improve your productivity.
In this tutorial, we have shown you the basic foundation of WP-CLI. Hopefully, now you can master several tasks using the command line, such as installing and managing WordPress, plugins, themes, posts, databases, and so on.
Good luck and be sure to comment below if you have any questions!