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 Rcs 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 Rcs 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 Rcs Marketplace. If unavailable on your server, install WP-CLI as described in the following steps.
Verify the installed PHP Version on your server.
$ php -vOutput:
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.pharVerify that the downloaded file is valid.
$ php wp-cli.phar --infoGrant execute privileges to run the file on your server.
$ sudo chmod +x wp-cli.pharMove the file to
/usr/local/bin/wpto activate thewpsystem-wide command.$ sudo mv wp-cli.phar /usr/local/bin/wpRun the following command to verify that WP CLI is available.
$ wp --infoOutput:
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/mysqlTo 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 versionOutput:
6.2.2To update WordPress to the latest version, run the following command.
$ wp core updateVerify the updated WordPress version.
$ wp core versionOutput:
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-checksumsIn a fresh directory, download new WordPress Core files.
$ wp core downloadThe 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.phpfile and establish a connection to your database, use the following syntax.$ wp config create --dbname=testing --dbuser=wp --dbpass=securepswd --locale=ro_ROFor example, to generate the file with
wp-db,example-user, andstrong-passwordas your database details, run the following command.$ wp config create --dbname=wp-db --dbuser=example-user --dbpass=strong-passwordTo establish a successful database connection, make sure the user exists in your MySQL database.
Verify that a new
wp-config.phpis available in your WordPress root directory.$ ls /var/www/htmlConfirm that the database lines match your set database information.
$ cat wp-config.phpUsing WPCLI, create the new database.
$ wp db createThe above command creates a new database on your MySQL database server. If the user in your
wp-config.phpfile 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 wordfenceMake 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 woocommerceTo deactivate a WordPress plugin using WP-CLI, for example
wordfence, run the following command.$ wp plugin deactivate woocommerceThe 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 listSearch themes by name in the WordPress marketplace, use the following syntax.
$ wp theme search <query>For example, search using the
Twentykeyword, run the following command.$ wp theme search TwentyTo install a new WordPress theme such as
Twenty Twenty-Three, run the following command.$ wp theme install twentytwentythreeThe above command installs the
Twenty Twenty-Threetheme from the WordPress marketplace, make sure you find and use the correct theme name to successfully use it.Activate the theme.
$ wp theme activate twentytwentythreeTo update all themes installed on your WordPress site, run the following command.
$ wp theme updateDeactivate a theme.
$ wp theme deactivate twentytwentythreeDelete a theme. For example,
Twenty Twenty-Three.$ wp theme delete twentytwentythreeVerify a theme's status on your WordPress website.
$ wp theme status twentytwentythreeOutput:
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-userwith the emailuser@example.com, andeditorprivileges, run the following command.$ wp user create example-user user@example.com --role=editorSet the user password.
$ wp user update example-user --user_pass=strong-passwordReplace,
strong-passwordwith your actual target password to set for the user.Verify that the new user is available.
$ wp user get example-userTo update the user permissions. For example, from
editortoadministrator, run the following command.$ wp user update example-user --role=administratorUpdate the user password.
$ wp user update example-user --user_pass=strong-passwordTo delete a WordPress user by username, run the following command.
$ wp user delete example-userView all available WordPress users.
$ wp user listThe 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.sqlOutput:
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.gzcopy of your WordPress files directory. Or, manually copy files without compression using thecpcommand as below.$ cp -r /var/www/html ~/WordPress-filesWhen 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:
PlainPost nameNumericDay and nameMonth 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 flushTo 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.phpwithhttps://example.com/about-us.php, run the following command.$ wp search-replace https://example.com/2023/about-us.php https://example.com/about-us.phpThe 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 downloadCreate the
wp-configfile.$ wp config create --dbname=wp-db --dbuser=example-user --dbpass=strong-passwordInstall WordPress using your
wp-configdetails.$ wp core install --url=wp.example.com --title="Site-Title" --admin_user=admin-user --admin_password=strong-password --admin_email=user@example.comReplace the URL
wp.example.com, website nameSite-Title, administrator usernameadmin,passwordwith a strong administrator password, anduser@example.comwith 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.