Introduction
Powering more than 60.8% of websites, WordPress is undoubtedly one of the most popular and powerful Content Management Systems (CMS).
Whether for personal blogs to enterprise sites, WordPress is very intuitive and easy to use both in development, design, and maintenance.
However, most WordPress users are only familiar with its graphical workflow; very few people have explored its terminal side.
Let us dive into the world of WP-CLI:
How to Install WP-CLI
Installing WP-CLI is relatively easy. The tool is in the form of a PHP archive which you can download and execute.
Start by downloading the archive using wget or cURL as:
wget https://raw.github.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
For cURL
users, use the command:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
Once downloaded, make the file executable and move the archive to a PATH in your system such as /usr/local/bin as:
chmod +x wp-cli.phar \
sudo mv wp-cli.phar /usr/local/bin/wp
To confirm that you have successfully installed it and it’s working, use the command:
wp --info
This should give you an output similar to the one shown below, indicating that you’ve installed the tool successfully.
NOTE: Ensure you have PHP installed; otherwise, you will get an env error.
OS: Linux 6.10.6-orbstack-00249-g92ad2848917c #1 SMP Tue Aug 20 15:46:01 UTC 2024 x86_64
Shell:
PHP binary: /usr/bin/php7.4
PHP version: 7.4.33
php.ini used: /etc/php/7.4/cli/php.ini
MySQL binary:
MySQL version:
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: /
WP-CLI packages dir:
WP-CLI cache dir: /root/.wp-cli/cache
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 2.11.0
How to Use WP-CLI
WP-CLI is a terminal or command line alternative to the wp-admin dashboard. Hence, there is a WP-CLI command for all the tasks you can perform with the WordPress admin web interface.
Let us learn how to use them, but before that:
How to enable WP-CLI Bash Completion
The WP-CLI tool allows you to enable the Bash Completion Feature to view and autocomplete commands using the TAB key.
Let us enable it to make our tasks easier.
We begin by downloading the Bash Completion Script with the command:
wget https://github.com/wp-cli/wp-cli/raw/master/utils/wp-completion.bash
To load the Bash Completion Script on every terminal session, let us edit our bash profile config file. Enter the following entry in the .bashrc file.
source $HOME/wp-completion.bash
Next, reload the bash profile to load all the changes:
source .bashrc
Once completed, you should have Bash Completion enabled. If you are using another shell, perhaps ZSH, check the official documentation for information on how to enable completion.
To test if it is working, enter the command wp + TAB. That should give you all available commands as:
cache core eval-file language option rewrite shell term cap cron export maintenance-mode package role sidebar theme cli db help media plugin scaffold site transient comment embed i18n menu post search-replace super-admin user config eval import network post-type server taxonomy widget
Installing WordPress with WP-CLI
Before we get to the WordPress admin, we have to install WordPress first. Let’s discuss how to install it using WP-CLI.
NOTE: Ensure you have a web server and MySQL database installed.
First, log in to MySQL shell and create a database
sudo mysql -u root -p
Enter Password:
Next, we need to create a database:
CREATE DATABASE wp;
Next, we need to create a user and grant all privileges as:
CREATE USER "wpadmin" IDENTIFIED BY "password";
GRANT ALL PRIVILEGES ON wp.* TO wpadmin;
FLUSH PRIVILEGES;
The next step is to download the WordPress installation file. For this, we are going to use the /var/www/html directory.
Change to /var/www/html
cd /var/www/html/
To ensure we have r/w
permission to that directory, chown
the www-data
user created by apache as:
sudo chown -R www-data .
sudo chown www-data:www-data .
Next, download WordPress using WP-CLI.
You will need to invoke the wp command as www-data
as the user has write permission to the /var/www/html
directory. Avoid using root.
sudo -u www-data wp core download
This will download WordPress and extract it into the current directory. Ignore the error shown below:
Downloading WordPress 6.6.1 (en_US)...
md5 hash verified: a15f676931133623b7b347f1fabc966b
Success: WordPress downloaded.
Confirm you have WordPress installed by listing the contents of the /var/www/html
directory:
Output:
-rw-r--r-- 1 root root 7387 Aug 26 15:16 wp-activate.php
drwxr-xr-x 1 root root 2804 Aug 26 15:16 wp-admin
-rw-r--r-- 1 root root 351 Aug 26 15:16 wp-blog-header.php
-rw-r--r-- 1 root root 2323 Aug 26 15:16 wp-comments-post.php
-rw-r--r-- 1 root root 3033 Aug 26 15:16 wp-config-sample.php
drwxr-xr-x 1 root root 44 Aug 26 15:16 wp-content
-rw-r--r-- 1 root root 5638 Aug 26 15:16 wp-cron.php
drwxr-xr-x 1 root root 10198 Aug 26 15:16 wp-includes
-rw-r--r-- 1 root root 2502 Aug 26 15:16 wp-links-opml.php
-rw-r--r-- 1 root root 3937 Aug 26 15:16 wp-load.php
-rw-r--r-- 1 root root 51238 Aug 26 15:16 wp-login.php
-rw-r--r-- 1 root root 8525 Aug 26 15:16 wp-mail.php
-rw-r--r-- 1 root root 28774 Aug 26 15:16 wp-settings.php
-rw-r--r-- 1 root root 34385 Aug 26 15:16 wp-signup.php
-rw-r--r-- 1 root root 4885 Aug 26 15:16 wp-trackback.php
-rw-r--r-- 1 root root 3246 Aug 26 15:16 xmlrpc.php
Next, we need to generate the WordPress configuration file and add the relevant information. Use the command below and replace the values appropriately.
sudo -u www-data \ wp core config --dbname="wp" --dbuser="wpadmin" --dbpass="password" --dbhost="localhost"
Output:
Success: Generated ‘wp-config.php’ file.
Once we have all the relevant configuration setup, we can finally run the installer setting up the WordPress user as:
sudo -u www-data \ wp core install --url="http://127.0.0.1" --admin_user="admin" --admin_password="password" --admin_email="admin@example.com" --title="WP-CLI Tutorial"
Output:
Success: WordPress installed successfully.
With that, you have WordPress installed on the system. You can test the site by navigating to http://localhost, which should display the default WordPress:
How to Manage a WordPress Site with CLI
Now you have an entire WordPress site installed and managed using WP-CLI. How about we try to perform basic tasks such as installing a plugin.
Install a Plugin with WP-CLI
While still in the WordPress site installation directory (/var/www/html), let us search for a plugin to install. Let us use the Elementor Page Builder as an example:
wp plugin search elementor
Running this command should give you all the possible plugins in tabular form.
Cool right? Now let us see how we can install the plugin once we find the appropriate name.
To install it, use the plugin slug as:
sudo -u www-data
wp plugin install elementor
Output:
Installing Elementor Website Builder (3.1.1)
Warning: Failed to create directory '/var/www/.wp-cli/cache/': mkdir(): Permission denied.
Downloading installation package from https://downloads.wordpress.org/plugin/elementor.3.1.1.zip...
Unpacking the package...
Installing the plugin...
Plugin installed successfully.
Success: Installed 1 of 1 plugins.
Once we have successfully installed the plugin we need, we can simply activate it using a similar command as:
sudo -u www-data
wp plugin activate elementor
Output:
Plugin ‘elementor’ activated.
Success: Activated 1 of 1 plugins.
Uninstall a Plugin with WP-CLI
If you can install a plugin with WP-CLI, you can uninstall it.
sudo -u www-data
wp plugin deactivate elementor
Output:
Plugin ‘elementor’ deactivated.
Success: Deactivated 1 of 1 plugins.
Once deactivated, you can uninstall it easily as:
sudo -u www-data
wp plugin uninstall elementor
Output
Uninstalled and deleted ‘elementor’ plugin.
Success: Uninstalled 1 of 1 plugins.
Installing WordPress Themes with WP-CLI
Themes are a common WordPress feature. Let’s discuss how to manage them from the command line.
To search for a theme, use the command:
wp theme search astra
Output:
Success: Showing 2 of 2 themes.
+--------+--------+--------+
| name | slug | rating |
+--——+--——+--——+
| Astra | astra | 100 |
| Astral | astral | 100 |
+--——+--——+--——+
Once you have the theme you wish to install, use the command as shown below:
sudo -u www-data
wp theme install astra
Output
Installing Astra (3.0.2)
Warning: Failed to create directory '/var/www/.wp-cli/cache/': mkdir(): Permission denied.
Downloading installation package from https://downloads.wordpress.org/theme/astra.3.0.2.zip...
Unpacking the package...
Installing the theme...
Theme installed successfully.
Success: Installed 1 of 1 themes.
Once you install the theme, you can activate it with the command:
sudo -u www-data
wp theme activate astra
Output:
Success: Switched to ‘Astra’ theme.
To install it from a zip file, use the command shown below:
sudo -u www-data
wp theme install oceanwp.2.0.2.zip
Output
Unpacking the package...
Installing the theme...
Theme installed successfully.
Success: Installed 1 of 1 themes.
Uninstalling a WordPress theme with WP-CLI
To remove a theme with CLI, first, activate another theme and then uninstall the one you want to uninstall using the command:
sudo -u www-data wp theme activate oceanwp && sudo -u www-data wp theme uninstall astra
Output:
Success: Switched to ‘OceanWP’ theme.
Deleted ‘astra’ theme.
Success: Deleted 1 of 1 themes.
View Themes and Plugins
To list all the themes and plugins in the WordPress instance, use the commands shown below:
wp theme list
wp plugin list
This command should list available themes and plugins respectively, as shown below:
+-----------------+----------+--------+---------+
| name | status | update | version |
+-----------------+----------+--------+---------+
| oceanwp | active | none | 2.0.2 |
| twentynineteen | inactive | none | 1.9 |
| twentytwenty | inactive | none | 1.6 |
| twentytwentyone | inactive | none | 1.1 |
+--—————+-———+--——+———+
Output
+---------+----------+--------+---------+
| name | status | update | version |
+---------+----------+--------+---------+
| akismet | inactive | none | 4.1.8 |
| hello | inactive | none | 1.7.2 |
+———+-———+--——+———+
Updating Themes and Plugins with WP-CLI
You can also update plugins and themes using the CLI. For example, to update all themes, use the command;
sudo -u www-data wp theme update --all
Output:
Success: Theme already updated.
NOTE: You can specify the specific theme name to update a single theme.
Updating WordPress from CLI
When the WordPress team releases a new version, you can update from the command line with a few single commands:
The first step is to update the site’s files first as:
sudo -u www-data
wp core update
Next, we need to update the database as:
sudo -u www-data
wp core update-db
Output:
Success: WordPress database already at latest db version 49752.
Creating a WordPress post with CLI
To create a post using WP-CLI, use the command below:
sudo -u www-data wp post create --post_type=page --post_title="WP-CLI Tutorial" --post_date="2024-05-06"
Output:
Success: Created post 5.
Deleting a Post
To delete a post, specify its numerical identifier as:
sudo -u www-data
wp post delete 5
Output:
Success: Trashed post 5.
Conclusion
This guide has shown you how you use the powerful WP-CLI to manage a WordPress site from the command line.
If you want to learn more about how to work with WordPress CLI, consider the documentation resource provided below: