In recent years, WordPress has been making advances in its development capabilities. The platform is becoming increasingly user-friendly, from automated unit testing to CSS preprocessing and command line tools. One of these features that stands out among the crowd is WP-CLI, an incredibly beneficial asset to any developer. In this article, we’ll take a closer look at what makes WP-CLI so valuable.
For experienced and enthusiastic web developers, this comprehensive guide will help you make your workflows more efficient using WP-CLI.
Table of Contents
- What is WP-CLI?
- Installing WP-CLI
- Updating WP-CLI
- Managing WordPress
- Installing WordPress
- Reinstalling WordPress
- Running WordPress Core Updates
- Rollback WordPress to a Previous Version
- Checking for Missing or Modified WordPress Core Files
- Managing Plugins
- Managing a WordPress Database
- Managing WordPress Users
- Performing a WordPress Database Search and Replace
- Changing WordPress URL
- Deleting WordPress Revisions
- Deleting WordPress Transients
- Flushing WordPress Permalinks
- Controlling Maintenance Mode
- Editing WordPress Configuration File
- Managing WordPress Cron
- Troubleshooting with WP-CLI
- Conclusion
What is WP-CLI?
WP-CLI, short for WordPress Command Line Interface, is a powerful tool that can be used to manage and maintain your WordPress website from the command line. It offers users a plethora of commands for various functions, like plugin installation, backup generation, core file updating, and many more.
Daniel Bachhuber has been the primary maintainer of the open source project known as ‘WordPress Command Line Interface (or, for short: ‘WP-CLI’) since 2003.
On August 8th, 2018, the new version of the WordPress Command Line Interface (known as “WPC-CLI v2”) became available.
For ChemiCloud customers, WP-CLI version 2.7.1 is readily available on all our Managed WordPress hosting plans; all you need to do is access your account via SSH, and you are good to go!
Installing WP-CLI
The instructions below will be helpful if you don’t have WP-CLI installed on your server. It is essential to ensure your system meets the necessary requirements before installing it. Specifically, users must have a UNIX-like environment (including OS X, Linux, FreeBSD, or Cygwin) and partial support in Windows environments. Moreover, they should have PHP 5.6+ and WordPress 3.7+; however, note that older versions of WordPress may provide lesser functionality.
Once you’ve verified the requirements, download the wp-cli.phar file using wget
or curl
:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
Next, check the Phar file to verify that it’s working:
php wp-cli.phar --info
To use WP-CLI from the command line by typing wp, make the file executable and move it somewhere in your PATH. For example:
chmod +x wp-cli.phar sudo mv wp-cli.phar /usr/local/bin/wp
If it was installed successfully, you should see something like this when you run wp --info
:
[root@rs9-nyc ~]# wp --info OS: Linux 4.18.0-425.10.1.lve.el7h.x86_64 #1 SMP Wed Jan 25 09:29:40 EST 2023 x86_64 Shell: /bin/bash PHP binary: /opt/cpanel/ea-php72/root/usr/bin/php PHP version: 7.2.34 php.ini used: /opt/cpanel/ea-php72/root/etc/php.ini MySQL binary: /usr/bin/mysql MySQL version: mysql Ver 15.1 Distrib 10.5.19-MariaDB, for Linux (x86_64) using readline 5.1 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: /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.7.1
For those using ChemiCloud Managed WordPress hosting, WP-CLI is available right away. Don’t know what version you’re working with? Utilize the wp cli version
command to get an answer.
[root@rs9-nyc ~]# wp cli version --allow-root WP-CLI 2.7.1
Updating WP-CLI
You can update WP-CLI by using wp cli update
or repeating the installation steps.
If WP-CLI is owned by root or another system user, you’ll need to run sudo wp cli update
.
Managing WordPress
Beginning a command requires wp
followed by the desired command, subcommand, and essential and nonessential parameters.
Keep in mind that whatever WordPress directory you are in the terminal is what WP-CLI will work with. If you are using cPanel as the control panel, here’s a great guide on how to find the document root of your WordPress website in cPanel. So, for instance, changing directories to enter a different one may cause you to use another installation of WordPress.
Now let’s explore some practical WP-CLI commands as well as associated examples.
Installing WordPress
To easily install WordPress using WP-CLI, here’s a list of commands that you should use:
# Download WordPress $ wp core download Downloading WordPress 6.2 (en_US)... md5 hash verified: 34f279efe623025641bc11a69e3c02fa Success: WordPress downloaded. $ wp core download --locale=nl_NL Downloading WordPress 4.5.2 (nl_NL)... md5 hash verified: c5366d05b521831dd0b29dfc386e56a5 Success: WordPress downloaded.
By default, WordPress will be installed using the en_US locale code. In case you wish to install WordPress in a different language, you can use the --locale
flag:
$ wp core download --locale=nl_NL Downloading WordPress 6.2 (nl_NL)... md5 hash verified: 34f279efe623025641bc11a69e3c02fb Success: WordPress downloaded.
Here’s a great guide containing a list of all the WordPress Locale codes. (Hint – You can CRTL / CMD + F to find your language).
The second command will set up the wp-config.php configuration file with the database connection details. If you are using cPanel, make sure to create a database first.
$ wp core config --dbname=database_name --dbuser=database_username --dbpass=database_password --dbhost=localhost --dbprefix=mywp_ Success: Generated 'wp-config.php' file.
The third command will install WordPress using a couple of parameters:
# Install WordPress $ wp core install --url=yourdomain.com --title="My WebSite Title" --admin_user=john.smith --admin_password=strongpassword [email protected] Success: WordPress installed successfully.
Reinstalling WordPress
You can reinstall WordPress core files using WP-CLI. The following command would download WordPress core without the default themes and plugins.
wp core download --skip-content --force
Running WordPress Core Updates
wp core version
# Display the WordPress version $ wp core version 6.2
wp core check-update
# Check for WordPress updates $ wp core check-update Success: WordPress is at the latest version.
wp core update
# Update WordPress to a newer version $ wp core update Updating to version 6.2 (en_US)... Downloading update from https://downloads.wordpress.org/release/wordpress-6.2-new-bundled.zip... Unpacking the update... Cleaning up files... No files found that need cleaning up. Success: WordPress updated successfully.
Rollback WordPress to a Previous Version
First, you’ll need to check on the previous WordPress version. You can find all the available versions here.
Suppose you want to roll back WordPress to the 6.0.3 version. The wp-cli command line that you should use is:
wp core update --version=6.0.3 --force
Checking for Missing or Modified WordPress Core Files
wp core verify-checksums
– this command verifies WordPress files against WordPress.org’s checksums. It is handy if you suspect your WordPress might be infected with malware or if there are missing WordPress core files.
$ wp core verify-checksums Warning: File should not exist: wp-admin/error_log Success: WordPress installation verifies against checksums.
Managing Plugins
Using wp plugin
you can easily manage plugins, including installs, activations, and updates.
wp plugin list
– get a list of installed WordPress plugins:
$ wp plugin list +---------+----------+--------+---------+ | name | status | update | version | +---------+----------+--------+---------+ | akismet | inactive | none | 5.1 | | hello | inactive | none | 1.7.2 | +---------+----------+--------+---------+
If you want to see only a list of all active plugins which have updates available, you can use the following:
wp plugin list --field=name --status=active --update=available
wp plugin install
– installs one or multiple WordPress plugins
To find the plugin names, you can visit the WordPress Repository and check out the URL. This is an easy way to get the name of a plugin from its repository.
Alternatively, you can use wp plugin search searchterm
which will give you a list of the slugs in the terminal.
# Install the latest version of the plugin and activate it: $ wp plugin install jetpack --activate Installing Jetpack – WP Security, Backup, Speed, & Growth (12.0) Downloading installation package from https://downloads.wordpress.org/plugin/jetpack.12.0.zip... Unpacking the package... Installing the plugin... Plugin installed successfully. Activating 'jetpack'... Plugin 'jetpack' activated. Success: Installed 1 of 1 plugins.
Another remarkable feature of WP-CLI is that it allows you to install WordPress plugins from remote files, not just the repository. This is incredibly useful when you’re working on a plugin or using a premium plugin.
$ wp plugin install http://s3.amazonaws.com/bucketname/my-plugin.zip?AWSAccessKeyId=123&Expires=456&Signature=abcdef --activate Downloading install package from http://s3.amazonaws.com/bucketname/my-plugin.zip?AWSAccessKeyId=123&Expires=456&Signature=abcdef Unpacking the package... Installing the plugin... Plugin installed successfully. Success: Installed 1 of 1 plugins.
To install multiple WordPress plugins, you can pile on parameters. Here’s an example that downloads and activates 3 plugins:
# Install multiple plugins: wp plugin install really-simple-ssl seo-by-rank-math litespeed-cache --activate
In some cases, installing a previous version of WordPress plugins may be necessary. To do so, use the --version=
flag
wp plugin install seo-by-rank-math --version=1.0.110 --activate
To get a previous version of a plugin, the first thing to do is search for the plugin in the WordPress repository. Scroll down on the plugin’s page and click on the “Advanced View.”
Scroll to the bottom of the page, and you will see an option to download previous versions of the plugin:
To deactivate a single plugin, you can run the following command:
$ wp plugin deactivate jetpack Plugin 'jetpack' deactivated. Success: Deactivated 1 of 1 plugins.
In the event of compatibility issues, it can be helpful to deactivate all plugins at once. Utilizing the command below makes it possible to do so quickly and easily. Afterward, you can reactivate them individually while testing for any conflicts.
To deactivate all your plugins at once, run the following command:
$ wp plugin deactivate --all Plugin 'hello' deactivated. Success: Deactivated 1 of 3 plugins.
You can also manually update WordPress plugins. Example below:
$ wp plugin update jetpack Success: Plugin already updated.
Additionally, you can update all your WordPress plugins. Example below:
$ wp plugin update --all Success: Plugin already updated.
Finally, you can also manually remove a WordPress plugin using the following command:
$ wp plugin delete jetpack Deleted 'jetpack' plugin. Success: Deleted 1 of 1 plugins.
Managing a WordPress Database
wp db
performs basic database operations using credentials stored in wp-config.php.
wp db export
exports the database to a file.
$ wp db export Success: Exported to 'cchostingdemos_wp342-2023-04-24-50121f9.sql'.
wp db import
imports a database from a file.
$ wp db import cchostingdemos_wp342-2023-04-24-50121f9.sql Success: Imported from 'cchostingdemos_wp342-2023-04-24-50121f9.sql'.
wp db optimize
optimizes the database.
$ wp db optimize Success: Database optimized.
wp db repair
repairs the database.
$ wp db repair Success: Database repaired.
wp db reset
removes all tables from the database.
$ wp db reset Are you sure you want to reset the 'cchostingdemos_wp342' database? [y/n] y Success: Database reset.
wp db search
finds a string in the database.
$ wp db search "[email protected]" mywp_options:option_value 6:[email protected] mywp_users:user_email 1:[email protected]
wp db size
displays the database name and size.
$ wp db size +----------------------+----------+ | Name | Size | +----------------------+----------+ | cchostingdemos_wp342 | 704512 B | +----------------------+----------+
wp db size --tables
displays the database tables and size for each.
$ wp db size --tables +-------------------------+---------+ | Name | Size | +-------------------------+---------+ | mywp_usermeta | 49152 B | | mywp_users | 65536 B | +-------------------------+---------+
wp db tables
lists the database tables.
$ wp db tables mywp_commentmeta mywp_comments mywp_links mywp_options mywp_postmeta mywp_posts mywp_term_relationships mywp_term_taxonomy mywp_termmeta mywp_terms mywp_usermeta mywp_users
Managing WordPress Users
wp user
manages users and their roles, capabilities, and meta.
The following command will list all the users on your WordPress install:
# List users wp user list # List users with administrator role wp user list --role=administrator
The following command will create a new user on your WordPress install. The available roles are administrator, editor, author, contributor, and subscriber.
# Create a new administrator user. wp user create john [email protected] --role=administrator Success: Created user 5. Password: C2p0MS4zCa
The following command will delete the user and re-assign all posts to another user:
# Delete user john and reassign posts to user paul $ wp user delete john --reassign=paul
By following the below steps, you will manage to reset the password for a specific WordPress user
Step 1: List all the users and find the ID of the user you'd like to change password: $ wp user list Step 2: Change password for specified user ID: $ wp user update 1 --user_pass=2DeBa10XjxD3Z8Ma replace "1" with the id of the user you want to change password.
The following command will help you change the email address for a specific WordPress user:
Step 1: $ wp user list # to see all users. Find the ID of the user you'd like to update email address Step 2: update the email address: $ wp user update 1 --user_email=<email> replacing "1" with the id of the user you want to update and <email> with the new email address.
The following command will help you set or modify a WordPress user role:
Step 1: $ wp user list # to see all users. Find the ID of the user you'd like to update. Step 2: $ wp user set-role 1 administrator Success: Added johndoe (1) to http://example.com as author. # Available roles: administrator editor author contributor subscriber
Performing a WordPress Database Search and Replace
wp search-replace
searches and replaces strings in the database. e.g.: replacing any match of an old domain name with a new domain name.
Before running this command, we strongly recommend you do a backup for your database using the following command line:
wp db export
Once you’ve ensured that you have a backup for the database, you can safely use the wp search-replace command:
wp search-replace 'https://old-domain.com/' 'http://new-domain.com/' --precise --recurse-objects --all-tables-with-prefix
Parameter | Description |
---|---|
–precise | Force using PHP (instead of SQL), which is more thorough but slower. |
–recurse-objects | Enable recursing into objects to replace strings. |
–all-tables-with-prefix | Enable replacement on any tables that match the table prefix even if not registered on $wpdb. |
If you want to run a test query without actually making changes, you can add the –dry-run parameter.
Exciting News: Introducing Our fastest WordPress Hosting stack 🚀 Ready for speed? Try it for just $1!
Changing WordPress URL
At times, you may need to modify your WordPress URL for various reasons. This could be due to changing domains, moving files, updating www to non-www, or even migrating from HTTP to HTTPS. Fortunately, the wp option update
command can simplify this process. To illustrate how it works, here is an example:
wp option update home 'https://mydomain.com'; wp option update siteurl 'http://mydomain.com'
Deleting WordPress Revisions
With a big website, the number of WordPress revisions can quickly become overwhelming. This is especially true when thousands of rows in your database are no longer needed. You can delete post revisions with WP-CLI. Here is an example of the command:
$ wp post delete $(wp post list --post_type='revision' --format=ids)
Deleting WordPress Transients
Transients are used by WordPress to manage the cache. Should you find yourself facing issues with cached pages or a plugin’s performance, it may be possible to resolve them simply by deleting the transients held in the database for your website. You can delete and clear out one or all transients using the following command:
wp transient delete --all
To delete expired transients, you can use the following command:
wp transient delete-expired
Flushing WordPress Permalinks
wp rewrite flush
resets WordPress’ rewrite rules based on registered post types.
Controlling Maintenance Mode
Using wp maintenance-mode
You can control the maintenance mode on your WordPress site. Example:
$ wp maintenance-mode status Maintenance mode is not active. $ wp maintenance-mode activate Enabling Maintenance mode... Success: Activated Maintenance mode. $ wp maintenance-mode deactivate Disabling Maintenance mode... Success: Deactivated Maintenance mode.
Editing WordPress Configuration File
The wp config set
command set sets the value of a specific constant or variable defined in wp-config.php file.
First, use wp config get
to get the value of a specific constant or variable defined in wp-config.php file:
$ wp config get +-------------------+-----------------------------------------------+----------+ | name | value | type | +-------------------+-----------------------------------------------+----------+ | table_prefix | mywp_ | variable | | DB_NAME | cchostingdemos_wp340 | constant | | DB_USER | cchostingdemos_wp340 | constant | | DB_PASSWORD | mystrongpassword | constant | | DB_HOST | localhost | constant | | DB_CHARSET | utf8 | constant | | DB_COLLATE | | constant | | WP_DEBUG | false | constant | +-------------------+-----------------------------------------------+----------+
Suppose we need to change the password for our WordPress database in the configuration file. The command that we’ll use is:
$ wp config set DB_PASSWORD database_password Success: Updated the constant 'DB_PASSWORD' in the 'wp-config.php' file with the value 'database_password'
Managing WordPress Cron
To list all scheduled cron events:
wp cron event list
This command displays all scheduled cron events with their hook names, next run time, and recurrence.
To run a specific cron event immediately:
wp cron event run <hook>
Replace <hook>
with the hook name of the cron event you wish to run.
To run all WordPress cron events that are due at the current time, you can use the following command:
wp cron event run --due-now
This command will execute only those cron events that are scheduled to run at the current time. It’s a useful way to trigger scheduled tasks immediately, especially for debugging or ensuring timely execution of critical tasks.
Troubleshooting with WP-CLI
Tackling WP-CLI can be daunting for novices, yet it is a must-have aptitude for experienced users and small business owners who require their websites to function optimally. Fortunately, several helpful commands make troubleshooting easier.
Enabling and Disabling Debugging Mode
Using wp config
, you can change the wp-config.php file, including setting the WP_DEBUG variable to true to enable debugging mode in WordPress.
Run the following command to enable WordPress Debugging:
$ wp config set WP_DEBUG true Success: Added the constant 'WP_DEBUG' to the 'wp-config.php' file with the value 'true'.
Once you have completed work on your website, it is essential to disable debugging. This will help protect the website by concealing any errors that may contain confidential data. To accomplish this, use false instead of true, following the steps above. For instance:
$ wp config set WP_DEBUG false Success: Updated the constant 'WP_DEBUG' in the 'wp-config.php' file with the value 'false'.
Skipping theme and plugin code then deactivate a plugin or a theme
If your site is encountering errors and cannot run commands, it may be necessary to skip the site’s active theme and plugin code. This is done by adding --skip-themes
and --skip-plugins
to any WP-CLI command.
Skip theme and plugin code then deactivate a plugin. Replace plugin-name
with a name
value found via wp plugin list
:
wp --skip-plugins --skip-themes plugin deactivate plugin-name
Skip theme and plugin code then activate another theme. Replace theme-name
with a name
value found via wp theme list
.
wp --skip-plugins --skip-themes theme activate theme-name
Deactivating all WordPress plugins at once
In the event of compatibility issues, it can be helpful to deactivate all plugins at once. Utilizing the command below makes it possible to do so quickly and easily.
To deactivate all your plugins at once, run the following command:
$ wp plugin deactivate --all Plugin 'hello' deactivated. Success: Deactivated 1 of 3 plugins.
Afterward, you can reactivate them individually while testing for any conflicts.
$ wp plugin activate hello Plugin 'hello' activated. Success: Activated 1 of 1 plugins.
Conclusion
WP-CLI is an incredibly powerful tool for managing WordPress sites, and it’s easy to see why so many developers have embraced it. WP-CLI provides a wealth of possibilities for those willing to invest time learning how to use it, from troubleshooting issues quickly to creating complex commands that automate tasks. With its wide range of useful commands and features, there’s no doubt that mastering wp-cli can be hugely beneficial when running a successful website or web agency.
We encourage you to try WP-CLI if you haven’t already tried it!
Don’t forget that ChemiCloud’s infrastructure supports WP-CLI out of the box. If you’d like to experience top speed and reliability and have access to great tools like WP-CLI, try our Managed WordPress hosting and have your website migrate today free of charge and with zero downtime.