Thanks to all the themes and plugins of WordPress, it is now very easy to build and manage a WordPress site. Once you have created your site, the Admin Panel allows you to manage it easily.
However, what if you prefer a command-line interface? It requires time and effort to manage WordPress websites through the Admin Panel. But, instead of taking steps manually, a command-line interface lets you quickly perform actions with simple commands.
That’s where WordPress Command Line Interface (WP-CLI) comes in. WP-CLI is a popular and powerful tool to generate backups, add/update themes and plugins, and manage your WordPress website using a simple command-line interface.
In this article, we’ll talk about what WP-CLI is and how you can use it to manage your WordPress website. We’ll also walk you through some of the basic WP-CLI commands:
- What Is WP-CLI?
- How To Install WP-CLI?
- Getting Started With WP-CLI Commands
- How to use WP-CLI from a remote server?
- Conclusion
What Is WP-CLI?
WP-CLI is an open-source tool for web developers to maintain and update their WordPress sites through the Command Line Interface. Its main goal is to speed up the WordPress developer’s workflow.
WP-CLI’s various functions and commands manage common (and some not-so-common) tasks such as resetting passwords, managing post categories, adding/removing users, updating plugins, configuring multisite installations, and more. WP-CLI saves you time and increases your productivity because it allows you to complete multiple tasks quickly.
Daniel Bachhuber has maintained WP-CLI since 2003. Over the years, WP-CLI has been a requirement for other open-source projects like Bedrock and Trellis. In January 2017, this open-source tool officially moved to WordPress.org and was co-maintained by Alain Schlesser.
Suggested read: How to Change the WordPress Admin Password via wp-cli
How To Install WP-CLI
Installing WP-CLI is easy. From WP-CLI.org, download the Phar file to complete the installation process. For other installation methods (Homebrew, Docker, Composer), check out the WP-CLI documentation file.
Make sure that your environment meets the following requirements:
- UNIX-like environment (OS X, Linux, FreeBSD, Cygwin); limited support in Windows environment
- PHP 5.4 or later
- WordPress 3.7 or later.
- Now, download the wp-cli.phar
- Download the cURL command-line tool.
- Check the Phar file to make sure it’s working by executing the following command in your Linux terminal after installing cURL:
php wp-cli.phar --info
To use the WP-CLI command, you need to first mark the file as an executable, as it’s still a Phar build. Then, you must move it in your PATH. Use the following command to make this happen:
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
Now, instead of typing wp-cli.phar, you can just type wp. When you run wp –info, you’ll see the following code, which means that WP-CLI is installed successfully:
$ wp --info
OS: Linux 4.19.128-microsoft-standard #1 SMP Tue Dec 7 12:58:10 UTC 2021 x86_64
Shell: /usr/bin/zsh
PHP binary: /usr/bin/php
PHP version: 8.0.5
php.ini used: /etc/php/8.0/cli/php.ini
MySQL binary: /usr/bin/mysql
MySQL version: mysql Ver 8.0.23-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))
SQL modes:
WP-CLI root dir: /home/wp-cli/
WP-CLI vendor dir: /home/wp-cli/vendor
WP_CLI phar path:
WP-CLI packages dir: /home/wp-cli/.wp-cli/packages/
WP-CLI global config:
WP-CLI project config: /home/wp-cli/wp-cli.yml
WP-CLI version: 2.5.0
If you did everything correctly, WP-CLI will work properly. If, however, you encounter an error, you’ll have to repeat the steps again.
To install WP-CLI on Windows, the process is slightly different. You should have the command line PHP for Windows in your system so you can execute PHP from any working directory.
Now, you can just use cURL to install WP-CLI (cURL ships natively with Windows 10).
Getting Started With WP-CLI Commands
In this section, we’ll learn some of the basic commands that are commonly used in WP-CLI.
WP-CLI commands generally begin with “wp.” A command and subcommand follow the “wp” to execute a query. A full WP-CLI command looks like this:
wp command subcommand requiredparam --optionalparam --optionalparam2=value
Let’s take a look at some basic WP-CLI commands with examples:
How To Install A Plugin With WP-CLI
Installing a plugin with WP-CLI is fairly straightforward. All RunCloud servers have WP-CLI installed by default, so you can easily connect to WP-CLI via the Terminal on Mac or through third-party software like PuTTY for Windows using your SSH key.
Once the connection is established, here’s what you do.
To install a public plugin through WP-CLI, type the following command:
wp plugin install [plugin name]
For example, to install the WooCommerce plugin, type:
wp plugin install woocommerce
To activate the plugin, type:
wp plugin activate [plugin name]
For example:
wp plugin activate woocommerce
Your plugin has now been activated.
Installing Multiple Plugins with WP-CLI:
You can install multiple plugins using the following command:
wp plugin install advanced-custom-fields jetpack ninja-forms --activate
To find a plugin name, use the command wp plugin search term, which will give you a list of plugins in the terminal.
You can also install plugins from a remote file. This comes in handy if you’re developing a plugin or using a premium plugin. The following example installs two plugins from the repository and one from an Amazon S3 server:
wp plugin install advanced-custom-fields jetpack https://d1qas1txbec8n.cloudfront.net/wp-content/uploads/2018/06/23073607/myplugin.zip --activate
This command is especially useful to troubleshoot compatibility issues. The best way to manage plugins that are not compatible is to deactivate all the plugins.
Get a list of plugins with WP-CLI
Run the following command to get the list of the available plugins:
wp plugin list
To see the list of active plugins, use the following commands:
wp plugin list --field=name --status=active --update=available
Managing WordPress themes with WP-CLI
To install a theme, type:
wp theme install [WordPress theme name]
For example:
wp theme install twentytwentytwo
Make sure to type the theme name exactly as it appears in the page URL.
To activate a theme, type:
wp theme activate twentytwentytwo
Your WordPress theme should now be activated.
To install and activate a theme from wordpress.org, run the following command:
$ wp theme install twentytwentytwo --activate
You can also install a theme from a local .zip file:
First, you have to download the ZIP file for your theme and upload it to your server. This is possible through SFTP using a client like FileZilla.
Once you’ve uploaded the file to your server, just run the following command:
$ wp theme install mynewtheme.zip -activate
Or from a URL:
$ wp theme install https://github.com/Automattic/_s/archive/master.zip
To list all installed themes, run the following command:
$ wp theme list
It’s important to note that non-marketplace themes can’t be downloaded using the WP-CLI. Custom themes can be activated through their zip folder or through URL. You could also download them with curl and the themes package will have specific instructions to activate them through WP-CLI.
Make sure to use only trusted sources to download themes and plugins as themes/plugins installed from unverified sources are not safe to use.
Installing WordPress with WP-CLI
You can easily install WordPress through WP-CLI by using a few simple commands. This allows you to skip the standard WordPress installation process which can take some time as compared to completing the installation process through WP-CLI.
Step 1: To install WordPress through WP-CLI, you’ll have to locate the directory where you want to complete the setup process and run the following command:
wp core download
Step 2: The second command will set up the config file with the database access and some additional PHP.
wp core config --dbname=mydbname --dbuser=mydbuser --dbpass=mydbpass --dbhost=localhost --dbprefix=whebfubwef
wp db create
wp core install –url=https://mysite.io –title=mysite –admin_user=mydbuser admin_password=mydbpass –[email protected]
Step 3: You’ll need to run the core install command to start the installation process. There are some extra parameters in the command such as email, admin username, and password, where you have to replace those parameters with your own variables.
WordPress should be installed to the latest version. After some time, you will need to update WordPress. You can use the following command to update the platform:
wp core update
Once the platform is fully updated, you’ll want to do the same for your database:
wp core update -db
Changing the WordPress URL with WP-CLI
Generally, WordPress is configured to work and accept requests through the URL which is set during the installation process. Sometimes, for various reasons, the URL is required to change. For example, you may need to move your WordPress installation from one subfolder/subdomain to a different one, or you might want to change your domain name. You may even need to force HTTPS connections to your website after installing an SSL certificate. Another reason may be when you want to install WordPress or want to move the site from local to production, then you need to update the WordPress and site address URLs.
Updating the URL is usually a complex job, as all the database entries containing the old URL need to be updated with the new URL. However, doing it through WP-CLI is much easier and quicker, and can be done by running one command:
wp option update home [URL homepage web address]
wp option update siteurl [URL homepage web address]
For example:
wp option update home 'http://www.domainofthewebsite.com'
wp option update siteurl 'http://www.domainofthewebsite.com'
Your WordPress URL should be updated.
There can be some hard-coded links to resources in your files that can contain old URLs. If you find any missing CSS files, missing images, or any other inconsistencies, check out your web browser’s error console for the particular errors. Also, you can open the error console by clicking F12.
Manage users with WP-CLI
You can use the following commands to manage users along with their roles, responsibilities, and metadata.
List users
In order to list all users you can use the following commands:
wp user list
You can also list users with certain roles (such as the administrator role in this case)
wp user list --role=administrator
Create a new user
New users can be created with the command:
wp user create name email.
If you want to assign the new user a role, then the command will need to include a suffix:
--role=whatevertheroleis
Examples:
Create a new editor role.
wp user create ron [email protected] --role=editor
Success: Created user 4.
Password: WordPressAssignedPassword
Delete user and re-assign posts to another user
Delete function is simple as create command – run command
wp user delete name
If you want to re-assign posts to other users, add the suffix:
--reassign=username
Example:
Delete user ‘ron’ and reassign posts to user ‘tim’:
$ wp user delete ron --reassign=tim
Reset the password for one user
This is how to reset the password of any user:
Step 1:
$ wp user list
Find the ID of the user you’d like to update.
Step 2: update the user
$ wp user update 1 --user_pass=password
Replacing “1” with the ID of the user you want to update.
Change admin email address for one user
Step 1:
$ wp user list
List to see all users. Find the ID of the user you’d like to update.
Step 2: update the user
$ wp user update 1 --user_email=newemail
replacing “1” with the ID of the user you want to update
Set or modify a user role
Step 1:
Type the following to see all users:
$ wp user list
Find the ID of the user you’d like to update.
Step 2:
$ wp user set-role 1 administrator
Success: Added ronsmith (1) to http://yourdomain.com as author.
WordPress’s available roles by default:
- Administrator
- Editor
- Author
- Manager
Suggested read: Using wp-cli to manage WordPress Cron Jobs
Manage WP core and cache with WP-CLI
Check for missing or modified core files
You can use the following commands to check for any missing or modified core WordPress files:
wp core verify-checksums
Reinstall WordPress Core
You can use the following command to download WordPress core without the default themes and plugins.
wp core download --skip-content --force
Flush cache
To flush the cache, use the following command:
wp cache flush
A full list of WP-CLI commands can be found by visiting https://developer.wordpress.org/cli/commands/.
How To Use WP-CLI From A Remote Server
One of the best features of WP-CLI is that you can use it to manage your remote WordPress installations.
You will have to install WP-CLI in two places: on the system from where you’re running the command, and on your server.
In order to install WP-CLI on your system, you can use the guide above since we have already gone through this. Saying that here’s how to configure the server with WP-CLI.
Configuring Remote Server with WP-CLI:
You can configure your server through WP-CLI globally or locally.
To configure your server globally, use the config.yml file. You can also use wp-cli.yml or wp-cli.local.yml files in your current working directory. In one of your working files, type the following code:
ssh:
staging:
cmd: ssh %pseudotty% [email protected] %cmd%
url: http://yourdomain.com
path: /www/path/to/site/root
Once this is done, write the following command to update WordPress on your server:
wp ssh core update --host=staging
Use Bash Scripts
Developers save up a lot of time by using bash scripts to automate tasks. With a single script, you can install WordPress or manage other tasks without typing multiple commands.
To use Bash scripts, you will have to build an “install.sh” file inside a directory and paste the following code:
wp core download
wp core config --dbname=mydbname --dbuser=mydbuser --dbpass=mydbpass --dbhost=localhost --dbprefix=whebfubwef_ --extra-php <<PHP
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
PHP
wp db create
wp core install --url=http://yourdomain.com --title=SiteTitle --admin_user=username --admin_password=yourpassword [email protected]
After which, enter the following command:
bash install.sh
The script will be good to go without any interference from the user. If you handle multiple websites, you can establish all your environments by creating similar bash scripts:
wp ssh core update --host=clientS
wp ssh core update --host=clientR
wp ssh core update --host=clientG
wp ssh core update --host=clientY
Bash scripts can free up your time when a new WordPress version is released.
After Action Report — Using WP-CLI Can Be Extremely Useful
WP-CLI solves many of the most common problems experienced by web developers and website managers. This saves you considerable time and effort, allowing you to focus elsewhere, safe in the knowledge your sites are taken care of.
As a web developer, you can quickly develop test sites, include test data, and import/export as required. You can easily manage site updates and different other jobs for various websites with one command. WP-CLI community commands such as WP-CLI-SSH also provide some amazing functions. If you encounter an issue, a good place to refer is the WP-CLI common issues documentation.
RunCloud easily allows hosts and lets you create and manage multiple WordPress websites with fast configuration. Using RunCloud in addition to WP-CLI, will give you back considerable time, and make site management easier.
What’s your favorite use case for WP-CLI? Did you come across any issues? Join the conversation & let us know in the comments below. 💬