How to Use the WP CLI to Manage Wordpress
Install, Configure and Use the WP-CLI with Wordpress
WP CLI or the Wordpress Command Line Interface makes several tasks easier for wordpress users. The CLI allows you to perform several tasks without having to open a browser or login to the wordpress admin dashboard to accomplish them.
From installing and updating a theme or plugin to creating posts, adding new users and several more tasks including your mysql database maintenance are all possible with the WP CLI.
All you need to do to use the WP CLI is to install it on your server and you can start using it. Instead of opening the admin dashboard in a browser and then performing a task from there in several clicks, just run a simple command from the terminal and you can easily manage your wordpress installation.
Another crucial use of the WP CLI is to maintain the wordpress database. You will not even need to login to phpmyadmin and you can delete transients, including expired transients or all transients and optimize tables at one go. If you need, you can easily reset your database with a single command using the WP CLI.
How to install and configure the WP CLI:
Installing and configuring the WP-CLI is rather a straightforward and simple process that can be completed in just two to three steps. The requirements for installing and using WP-CLi are as follows:
A linux server since support for the windows environment is limited.
Use the latest php version (Any version works but you must use php 8.0 or above)
Wordpress latest version is 6.2.2 and you are advised to use the latest version.
The recommended method for installing the WP-CLi is to download the phar file and install on your server or locally.
SSH to your server. Ssh root@serverip.
Now download the phar file using Wget or curl.
Run the following command to download and install the WPCLI.
$ curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
Now, you can check to verify that the CLI works. Type the following command in the terminal:
$ php wp-cli.phar –info
However, you do not want to type wp-cli.phar every time to run the WP CLI commands. You can use the CLI by simply typing wp from the command line. To do that you need to make the file executable and move it to somewhere in your PATH. Run the following command:
$ chmod +x wp-cli.phar $ sudo mv wp-cli.phar /usr/local/bin/wp
To verify that the WP-CLI has been successfully installed, run the following command:
$ wp –info
An output like the following shows that the CLI was installed successfully and you can start using it.
root@wordpress:~# wp --info
OS: Linux 5.15.0-67-generic #74-Ubuntu SMP Wed Feb 22 14:14:39 UTC 2023 x86_64
Shell: /bin/bash
PHP binary: /usr/bin/php8.2
PHP version: 8.2.8
php.ini used: /etc/php/8.2/cli/php.ini
MySQL binary: /usr/bin/mysql
MySQL version: mysql Ver 8.0.32 for Linux on x86_64 (MySQL Community Server - GPL)
SQL modes: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE, ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
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: /root
WP-CLI packages dir:
WP-CLI cache dir: /root/.wp-cli/cache
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 2.8.1
Now, you can use the WP-CLI for running several types of tasks in both single site and multi site environments. Whenever, you need to update the CLI, you can do so by running the command
$ wp cli update ‘or’
$ sudo wp cli update
The command will check for available updates and install if any. Otherwise, you will get the message that the latest version is installed already.
WP CLI global Parameters
WP CLI has a series of global parameters whose behavior does not change with commands. It means they serve the same purpose whichever command you use them with and that’s why they are called global parameters. They affect how the CLI interacts with wordpress.
For example, to run a WP CLI command, you need to specify the path to the root folder. However, it can be skipped if you are already inside the root folder. So, you can either change directory to the root folder or just add the –path= parameter to specify the root folder. You can add it to any WPCLI command.
Similarly, to specify a user and run commands as that user, you can add the –user parameter.
Below is a list of global parameters that can be used with WP CLI commands:
- –path=
For path, you can specify the path to the root folder, which is generally located at /var/www/html.
- –SSH=[@][:][] This parameter is used to perform operation over a remote server using SSH. For example:
$ wp cache flush –[email protected]:80 /var/www/html
–user=<id|login|email> : Used to set the default wordpress user.
–skip-plugins[=]: User to skip the loading of some or all of the plugins. For example; –skip-plugins=wp-seo
–skip-themes[=]: Used to skip loading all or some themes. For example; –skip-themes-twentynineteen
There are more similar global parameters that you can get a list from the wordpress website.
Common Tasks Performed with the WP-CLI
Here are some of the most common and basic tasks that you can easily perform using the WP CLI. (A crucial benefit of using the WP CLI is that you can avoid the use of plugins in this way, since some of these tasks might otherwise require installing a different plugin.)
Creating new users or deleting existing ones:
Adding and deleting users can be easier with the WP CLI. There is a simple command - wp user create - to create a new user. Not just thus you can list existing users and their ids as well as date of registration and email with the - wp user list - command. Suppose you want to add a new user mike. Run the command:
$ wp user create mike [email protected] –role=author
The output of the above command will include the password for the user you just created. In the same manner you can delete a specific user with his id and assign the posts written by that user to another through the WP CLI.
Creating and deleting posts:
You can add posts or delete existing posts using the WP CLI. To create a new post, you can use the wp post create command which will create a new post and assign the title you provided. If you want to edit an existing post, you can do so using the WP CLI. The command wp post edit launches the system editor for editing a given post. You will only need to add the post id to the command to start editing it For example; wp post edit 123.
If you want to check out the posts published on your blog, you can list them using the wp post list command. It will list all the posts including the ones that have been published or are in draft mode as well as the slugs, titles, ids and status.
You can select the post id you want to edit and then launch the editor.
Installing and updating plugins and themes:
You can run timely updates using the WP CLI. Whether it is a plugin or a theme you can install it and update it or even delete it using the WP CLI. The wp theme install command installs a theme and you can activate it using the –activate flag. For example; install and activate the twentyseventeen theme with the following command:
$ wp theme install twentyseventeen –activate
To delete the same theme, run the following command:
$ wp theme delete twentyseventeen
If you have an existing theme on your wordpress blog and want to enable or disable it, that can be done using wp theme enable and wp theme disable commands. To list all the themes on your site, just run the command: wp theme list. Update an existing theme using the wp theme update command. For example; wp theme update twentyseventeen.
Maintaining mysql database
The mysql database plays a critical role in running a wordpress blog. It stores all the information including posts, plugins, themes and media on your wordpress blog and also plays a critical role in wordpress speed. It is why ongoing maintenance is essential for the wordpress database to ensure the wordpress site runs faster.
Suppose, you need to do a sitewide search and replace. If you do not know how to use the wp-cli, you will need to install a dedicated search and replace plugin for it. However, the same task can be performed using the wp-cli with much more ease and without even touching the dashboard. You will just need to run the wp search-replace command to search and replace a particular string in the wp mysql database.
No need to install a plugin or to login to phpmyadmin. This command will search the entire database and do the necessary changes within seconds. If you want to check out how many changes need to be performed for replacing a specific string before making the real changes in the database, use the –dry-run flag. However, when working with the database make sure to take a backup first so you can upload the backup if anything goes wrong. The wp db export command comes in handy when you need to take a backup.
Similarly, you can use the WP-CLI to run database optimization and to clear transients, whether expired transients or all transients.
Managing comments (approving, unapproving, deleting comments)
Managing comments can be a bit cumbersome when you are trying to do it from the wp admin dashboard. However, you can do it with ease using the WP CLI. First of all, you can easily list all the comments on your blog or an individual post using the wp comment list command.
The wp comment approve command will approve a comment whereas wp comment spam will mark it as spam. You can delete a comment with wp comment delete or unapprove it using wp comment unapprove.
Edit Wordpress configuration file wp-config:
The wp config file contains the wordpress configurations including wp mysql database credentials and other critical information. It plays a critical role in the functioning of wordpress and its security.
You are advised to take a backup of the wp-config.php file before editing it. To generate a wp config file automatically, you can use the command wp config create. If you run the command wp config edit, it will open the system editor and you can make the necessary changes to the file. However, to delete certain variables from the file, you can just run wp config delete.
Flush object cache:
Flushing the object cache from wordpress is pretty easy using the WP CLi. All you need to do is to run the command wp cache flush. You will see the following output :
Success: The cache was flushed.
Regenerate thumbnails.
Many times users have to install a dedicated plugin if they want to regenerate thumbnails for media images. USing the wp cli, you can achieve that with a single command. Run the following command to regenerate thumbnails:
$ wp media regenerate –yes
This will regenerate thumbnails for all the media images without having to give any confirmation. In case you want to set a featured image for a specific post, it can be done using. You will need to run the wp media import command with additional parameters, which will upload the file and set it as the featured image for the post specified.
Manage Widgets:
Managing widgets is also pretty simple with the WP command line interface. To check out the widgets installed on a particular sidebar you just need to run the command like:
$ wp widget list sidebar-1
To add a widget to a sidebar just run the command wp add widget and mention the sidebar. For example: wp widget add search sidebar-1. To delete a widget run the command wp widget delete. You can also delete more than one widgets from the sidebar at once.
A few last words:
The WP CLI makes it a lot easier to manage and maintain your wordpress installation and your mysql database without having to login to the dashboard and just through the use of terminal commands.
The CLI also makes it possible to accomplish some complex tasks easily without having to install a dedicated plugin each time. For example, you do not need a plugin to regenerate thumbnails, or to carry out search and replace and for managing database.
Sometimes, if you need to scan through all the post titles on your blog, you can either do it from the admin dashboard where you will need to scan several pages manually or you can generate a list from the WP CLI. The WP CLI can be a time saver and in many instances a life saver.
Especially, the features like search and replace or similar more features that make it easier to manage database and cron jobs can be highly helpful for the users who can run commands from the terminal. So, use the WP-CLi to make wordpress management and maintenance easier for you. However, in certain instances you need to exercise some precaution when dealing with the database and sensitive files like the wordpress configuration file.