Manage your WordPress website with WP-CLI
Introduction
WordPress Command Line Interface (WP CLI) is a powerful terminal-based tool that lets you take control of your WordPress site functionality in a single session. As a popular Content Management System (CMS), WordPress is best used through its user-friendly web dashboard, but graphical interfaces are subject to in-browser challenges, and security threats addressed by the WP CLI tool.
Key advantages of using WP CLI to manage your WordPress site include:
Enhanced Productivity
WP-CLI offers improved productivity and focus on task execution with direct commands. This is important for backend developers as all tasks run in the background, and frontend users such as editors access the changes in real time without delays.
Improved Administrator Account Security
Through the WordPress graphical dashboard, administrative accounts are subject to security threats such as key logging, and data collection browser extensions. WP CLI offers greater control with access to user account management, password, and your website security controls through a secure shell connection.
Advanced Control
WP-CLI allows you to manage your WordPress themes, plugins, and core features making it easy to troubleshoot the website in case of critical errors and failures on your website.
Task automation and Scheduling
WP-CLI is compatible with system CronJobs which scheduling of tasks such as full site backups, and feature upgrades on the server.
This article explains how you can manage your WordPress website using WPCLI. You will perform common WordPress tasks such as managing user permissions, then, install and update features such as plugins, and themes on your website.
Prerequisites
Before you begin, be sure to:
Deploy a WordPress Server using the OneClick Vultr Marketplace Application.
If you have an existing WordPress website, verify that you have SSH access to the server. When using a web control panel such as CPanel or Plesk, migrate your WordPress server to Vultr for full access to your site and use WP-CLI.
Using SSH, access the server.
Create a non-root user with sudo privileges, and switch to the account.
Install WP-CLI
By default, WP-CLI is activate and available on WordPress servers deployed through the Vultr Marketplace. If unavailable on your server, install WP-CLI as described in the following steps.
Verify the installed PHP Version on your server.
$ php -v
Output:
PHP 8.1.21 (cli) (built: Jul 12 2023 23:03:54) (NTS)
To use WP-CLI, the installed version must be PHP 7 and above.
Download the latest WP-CLI release file.
$ curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
Verify that the downloaded file is valid.
$ php wp-cli.phar --info
Grant execute privileges to run the file on your server.
$ sudo chmod +x wp-cli.phar
Move the file to
/usr/local/bin/wp
to activate thewp
system-wide command.$ sudo mv wp-cli.phar /usr/local/bin/wp
Run the following command to verify that WP CLI is available.
$ wp --info
Output:
OS: Linux 5.15.0-71-generic #78-Ubuntu SMP Tue Apr 18 09:00:29 UTC 2023 x86_64 Shell: /bin/bash PHP binary: /usr/local/lsws/lsphp81/bin/php PHP version: 8.1.21 php.ini used: /usr/local/lsws/lsphp81/etc/php/8.1/litespeed/php.ini MySQL binary: /usr/bin/mysql
To update WP-CLI to the latest version, run the following command.
$ wp cli update
WP CLI Commands
You can access WP-CLI commands by running the wp help
command to view executable commands. Among the commonly used commands, you can use the following WP-CLI commands to use the tool effectively.
wp help
: View available WP CLI commands.wp core download
: Download the latest WordPress core files.wp db create
: Create a new database.wp plugin install
: Install a plugin from the WordPress marketplace.wp theme install
: Install a WordPress theme.
To view more WP-CLI commands, run the help command as below.
$ wp help
Manage WordPress Using WP CLI
In this section, use WP CLI commands to manage your WordPress site. Update WordPress, manage core files, install and activate plugins, manage themes, and install WordPress files on a new domain as described in the steps below.
To correctly use WP-CLI, switch to your WordPress webroot directory. For example /var/www/html
.
$ cd /var/www/html
Depending on your WordPress installation, make sure to use WP-CLI in your WordPress files directory, or define the path when running the command as below.
Update WordPress Using WP-CLI
When your WordPress version needs an update to the latest version, update WordPress using WP-CLI in the steps below.
To avoid any data loss:
Verify your installed WordPress version.
$ wp core version
Output:
6.2.2
To update WordPress to the latest version, run the following command.
$ wp core update
Verify the updated WordPress version.
$ wp core version
Output:
Success: WordPress is up to date.
Manage WordPress Core Files
WordPress core files consist of the most important files that allow WordPress to run on your server. These include wp-admin
, wp-includes
, index.php
, and the wp-config.php
file you can update as described below.
Verify the integrity of WordPress core files.
$ wp core verify-checksums
In a fresh directory, download new WordPress Core files.
$ wp core download
The above command downloads the full WordPress core files. It’s important to re-download the files in case your WordPress site crashes and requires new files.
To generate a new
wp-config.php
file and establish a connection to your database, use the following syntax.$ wp config create --dbname=testing --dbuser=wp --dbpass=securepswd --locale=ro_RO
For example, to generate the file with
wp-db
,example-user
, andstrong-password
as your database details, run the following command.$ wp config create --dbname=wp-db --dbuser=example-user --dbpass=strong-password
To establish a successful database connection, make sure the user exists in your MySQL database.
Verify that a new
wp-config.php
is available in your WordPress root directory.$ ls /var/www/html
Confirm that the database lines match your set database information.
$ cat wp-config.php
Using WPCLI, create the new database.
$ wp db create
The above command creates a new database on your MySQL database server. If the user in your
wp-config.php
file does not exist on the server, the command fails. Hence, WP-CLI helps you change the database, and use your user connection to access the database server.
Install and Activate WordPress Plugins
To install a new WordPress plugin, for example
wordfence
, run the following command.$ wp plugin install wordfence
Make sure you use the correct plugin name to correctly install it in WordPress. You can find all available plugins in the WordPress marketplace.
Activate the plugin.
$ wp plugin activate woocommerce
To deactivate a WordPress plugin using WP-CLI, for example
wordfence
, run the following command.$ wp plugin deactivate woocommerce
The above command deactivates the plugin, and you can further delete the plugin from your WordPress installation by running the following command:
$ wp plugin delete woocommerce
Manage WordPress Themes
View all installed themes on your WordPress site.
$ wp theme list
Search themes by name in the WordPress marketplace, use the following syntax.
$ wp theme search <query>
For example, search using the
Twenty
keyword, run the following command.$ wp theme search Twenty
To install a new WordPress theme such as
Twenty Twenty-Three
, run the following command.$ wp theme install twentytwentythree
The above command installs the
Twenty Twenty-Three
theme from the WordPress marketplace, make sure you find and use the correct theme name to successfully use it.Activate the theme.
$ wp theme activate twentytwentythree
To update all themes installed on your WordPress site, run the following command.
$ wp theme update
Deactivate a theme.
$ wp theme deactivate twentytwentythree
Delete a theme. For example,
Twenty Twenty-Three
.$ wp theme delete twentytwentythree
Verify a theme's status on your WordPress website.
$ wp theme status twentytwentythree
Output:
Theme twentytwentythree details: Name: Twenty Twenty-Three Status: Active Version: 1.1 Author: the WordPress team
Manage WordPress Users Using WP-CLI
WordPress users have 5 permission levels, administrator
, editor
, author
, contributor
and subscriber
. You can set and manage WordPress users using WP-CLI as described in the steps below.
To create a new WordPress user, follow the syntax below.
$ wp user create <username> <email> --role=<role>
For example, to create a new user
example-user
with the emailuser@example.com
, andeditor
privileges, run the following command.$ wp user create example-user user@example.com --role=editor
Set the user password.
$ wp user update example-user --user_pass=strong-password
Replace,
strong-password
with your actual target password to set for the user.Verify that the new user is available.
$ wp user get example-user
To update the user permissions. For example, from
editor
toadministrator
, run the following command.$ wp user update example-user --role=administrator
Update the user password.
$ wp user update example-user --user_pass=strong-password
To delete a WordPress user by username, run the following command.
$ wp user delete example-user
View all available WordPress users.
$ wp user list
The above command lists all users available on the WordPress website.
Backup WordPress Using WP-CLI
To back up your WordPress website using WP-CLI, back up the site database, and use compression tools such as tar
or zip
to compress a copy of your web root directory as described below.
Back up your WordPress database to a safe destination such as your home directory.
$ wp db export ~/wp-backup.sql
Output:
Success: Exported to '/home/hum/backup.sql'.
Using
tar
, create a new compressed copy of your WordPress webroot directory.$ sudo tar -czf ~/wordpress_backup.tar.gz /var/www/html/
The above command creates a new compressed
tar.gz
copy of your WordPress files directory. Or, manually copy files without compression using thecp
command as below.$ cp -r /var/www/html ~/WordPress-files
When complete, verify that the new files are available in your destination directory. For this article, your user home directory.
$ ls ~/
Search and Replace WordPress permalinks Using WP-CLI
When making changes on your WordPress site, permalinks may change, and some URLs may incorrectly display errors when loaded. To update your WordPress permalinks, follow the steps below.
Update your WordPress permalink structure using the syntax below.
$ wp rewrite structure <permalink>
The WordPress Permalink structure consists of:
Plain
Post name
Numeric
Day and name
Month and name
Depending on your desired URL structure, your set structure works in the format: https://example.com/<permalink>
For example, to set your new permalinks to
post name
, run the following command.$ wp rewrite structure /%postname%/
Refresh the WordPress rewrite rules to save changes.
$ wp rewrite flush
To update your WordPress permalinks per URL, follow the syntax below.
$ wp search-replace <‘old’> <‘new’>
To replace the old URL
https://example.com/2023/about-us.php
withhttps://example.com/about-us.php
, run the following command.$ wp search-replace https://example.com/2023/about-us.php https://example.com/about-us.php
The above command finds the old URL and replaces it with the new URL.
Visit your WordPress website to verify changes in the URL structure.
https://example.com
Install WordPress on a fresh Domain Using WP-CLI
To install WordPress on a new domain or subdomain, make changes to your web server configurations, create a new MySQL database user, then, use WP-CLI to install WordPress as described below.
Switch to the new domain’s web root directory. For example
/var/www/wp-example
.$ cd /var/www/wp-example/
Using WP-CLI, download the WordPress core files.
$ wp core download
Create the
wp-config
file.$ wp config create --dbname=wp-db --dbuser=example-user --dbpass=strong-password
Install WordPress using your
wp-config
details.$ wp core install --url=wp.example.com --title="Site-Title" --admin_user=admin-user --admin_password=strong-password --admin_email=user@example.com
Replace the URL
wp.example.com
, website nameSite-Title
, administrator usernameadmin
,password
with a strong administrator password, anduser@example.com
with your actual details to save in your WordPress website configuration.In a web browser, visit your domain name and verify that your WordPress website is available.
https://wp.example.com
Conclusion
WP-CLI is a powerful WordPress management tool that lets you take control of your website backend. It’s an important tool for developers and site administrators to effectively perform several functions on the WordPress website. For more information about the tool, visit the WP-CLI documentation.
Next Steps
WordPress is a highly extensible Content Management System (CMS). To apply more configurations and fine-tune your website, visit the following resources.
- Migrate WordPress Websites from cPanel or Plesk to Vultr
- WordPress Database Backend: Managed MySQL vs Self Hosted
- Serve WordPress Assets using Vultr Object Storage
- WordPress Object Cache with Vultr Managed Database for Caching
- Backup a WordPress Website using 3-2-1 Strategy
- Manage your WordPress Website with WP-CLI
- Bring WordPress to the Edge with Vultr and GeoDNS
- Harden WordPress Security with Vultr and Cloudflare
- Common WordPress Troubleshooting Guide