WP-CLI or WordPress Command Line Interface is a suite of commands that can make quick work out of updating plugins, setting up multisites, and much more WordPress management tasks, right from the Command Line Interface (CLI).
NOTE: InMotion Hosting’s
WordPress Hosting includes WP-CLI and is already installed. If you are already familiar with working in a Command Line Interface (CLI), check out our
WP-CLI Education Channel to learn about the commands available using WP-CLI.
Prerequisites for WP-CLI
Before we dive into installing WP-CLI, let’s make sure you’ve got everything you need to get started:
- PHP version: First up, you’ll need PHP installed on your server, as WP-CLI is built to work with WordPress, which runs on PHP. Specifically, you’ll want PHP 7.4 or higher – this keeps things compatible and secure. If you’re unsure about your PHP version, a quick check with your hosting provider or a command like php -v in your terminal will clear things up.
- SSH access: You’ll need SSH access to your server to run WP-CLI commands. Most hosting providers, like InMotion Hosting, offer this—double-check with your host to ensure you’ve got the credentials handy.
- WordPress installation: Another key requirement is WordPress itself. WP-CLI is designed to manage WordPress sites, so you’ll need at least one WordPress installation ready to go. WP-CLI works with WordPress 3.7 or higher, but we recommend using a recent version for security and performance reasons.
- cURL and wget: These tools are usually pre-installed on most servers, but WP-CLI may use them for downloading files. Run curl– version or wget– version to confirm they’re available.
- Sufficient permissions: Make sure your user account has the necessary permissions to execute commands and modify WordPress files on the server.
Install WordPress WP-CLI
- SSH into your server as the root user.
- You will need to download wp-cli.phar using either wget or curl.
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
or
wget https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
NOTE: Using wget, if you get an error about the SSL certificate mis-match between www.github.com and raw.githubusercontent.com domains, you can skip the certificate check to download the file with:
wget –no-check-certificate https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
- Then test WP-CLI directly with PHP with this command:
php wp-cli.phar –info
You should see output similar to:
PHP binary: /opt/cpanel/ea-php70/root/usr/bin/php PHP version: 7.0.26 php.ini used: /opt/cpanel/ea-php70/root/etc/php.ini WP-CLI root dir: phar://wp-cli.phar WP-CLI packages dir: WP-CLI global config: WP-CLI project config: WP-CLI version: 0.24.1
- Next you’ll need to make the WP-CLI file executable and move it to your path:
chmod +x wp-cli.phar
mv wp-cli.phar /usr/local/bin/wp
- Now you can simply use wp at the command line to access WP-CLI:
wp –info
Set up Bash Tab-to-Complete for WP-CLI
A great time-saving feature of command line interfaces is using tab completion to quickly type in commands. You can add the ability to use tab completion for WP-CLI by setting it up in your local ~/.bash_profile file.
- SSH into your server as the user that will be using WP-CLI.
- Download the WP-CLI tab completion file to your server.
curl -O https://raw.githubusercontent.com/wp-cli/wp-cli/master/utils/wp-completion.bash
or
wget https://raw.githubusercontent.com/wp-cli/wp-cli/master/utils/wp-completion.bash
NOTE: While using
wget, if you get an error about the SSL certificate mis-match between
www.github.com and
raw.githubusercontent.com domains, you can skip the certificate check to download the file with:
wget –no-check-certificate https://raw.githubusercontent.com/wp-cli/wp-cli/master/utils/wp-completion.bash
- Move the WP-CLI tab completion file to your home directory:
mv wp-completion.bash ~/
- Now edit your ~/.bash_profile file to include the WP-CLI tab completion file:
vi ~/.bash_profile
- Add this line to the bottom of your ~/.bash_profile file:
source ~/wp-completion.bash
Now save this file.
To verify that WP-CLI is installed correctly, you can run the command: wp –info. This will display environment-specific information about WP-CLI, including the version, PHP version, WordPress version, and system details.
Example of WP-CLI wp --info
Output:
OS: Linux 5.15.0-105-generic #115-Ubuntu SMP x86_64
Shell: /bin/bash
PHP binary: /usr/bin/php
PHP version: 8.1.2
php.ini used: /etc/php/8.1/cli/php.ini
WP-CLI root dir: phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI version: 2.9.0
- Back at the command line, make sure to source your ~/.bash_profile file by running this command:
source ~/.bash_profile
- Now just type wp at the prompt, and hit Tab twice to see all the tab completions you just set up:
exampl3@server [/home/exampl3/public_html]# wp
cache comment eval help menu plugin role shell super-admin transient
cap core eval-file import network post scaffold sidebar term user cli
db export media option rewrite search-replace site theme widget
Now that you have installed WP-CLI you can begin using the command line to manage your WordPress website(s).
Configuring Your Command-line Interface
If you’re regularly working with WordPress via the command line, you’ve probably found yourself typing the same flags or options over and over. It can get tedious, and that’s where the WP-CLI configuration file, wp-cli.yml, comes in.
What is wp-cli.yml?
The wp-cli.yml file is a simple YAML configuration file that lets you define default options and settings for your WP-CLI commands. Instead of repeating the same parameters, you set them once and forget them. WP-CLI reads this file automatically. Thus, it saves you time and reduces human error.
Why Use a Configuration File?
Imagine you’re always running wp core update with –path=/var/www/html/mysite. Instead of typing that every single time, you can store it in your wp-cli.yml file like this:
- path: /var/www/html/mysite
Now, every WP-CLI command you run from that directory will default to that path—no extra typing needed.
How to create a wp-cli.yml file?
- Choose a location: WP-CLI supports multiple levels of config files, so you can create one in:
- The current working directory (for project-specific settings)
- Your home directory (~/.wp-cli/config.yml for global settings)
- Inside the WordPress root directory
- Create the file: Use any text editor to create a file called wp-cli.yml. Here’s a basic example:
- path: /var/www/html/mysite
- url: https://mysite.com
- user: admin
- Save it and test: Once saved, WP-CLI will automatically read the file. Try running a command like wp plugin list without specifying the path or URL – it’ll just work.
Useful Command Tips
Tired of digging through documentation or typing long commands? Here’s a list of the most essential WP-CLI commands every WordPress developer should have in their toolkit.
- Core management:
- wp core version – quickly see installed WP version
- wp core update – update WordPress core to latest
- wp core install – install WP with URL, title, admin creds
- wp core verify-checksums – check if core files were modified
- Plugin/theme management:
- wp plugin list – view all plugins and their status
- wp plugin install plugin-slug –activate – install & activate in one go
- wp plugin update –all – update every plugin at once
- wp theme install theme-slug –activate – install and activate a theme
- wp theme update –all – update all themes
- User management:
- wp user list – list all users with roles and IDs
- wp user create username [email protected] –role=editor – add a new user
- wp user update 2 –user_pass=newpassword – reset user password
- Database operations:
- wp db export – quick backup of your database
- wp db import file.sql – restore from a SQL file
- wp db query “SELECT * FROM wp_users;” – run raw SQL
- wp search-replace ‘oldurl.com’ ‘newurl.com’ – find & replace across DB
Updating WP-CLI
Update WP-CLI by running wp cli update in your terminal. Ensure you have proper permissions or use sudo if needed.
Troubleshooting WP-CLI
1. Permission Issues in Shared Hosting
Shared hosting environments often impose strict permissions, which can cause WP-CLI to fail. Common issues include a lack of write access to WordPress directories or restricted shell access.
Solution
- Ensure the WordPress directory has the correct file permissions (755 for directories, 644 for files). If necessary, adjust them using FTP or a file manager.
- The user running WP-CLI should have the same ownership as the WordPress files. If not, contact your hosting provider to correct ownership or grant necessary permissions.
- Some shared hosting environments restrict shell access. If needed, request SSH access from your hosting provider to use WP-CLI effectively.
2. PHP Version Compatibility Problems
WordPress and WP-CLI require a minimum PHP version to work correctly. Sometimes, your host may run an outdated PHP version, which causes compatibility problems with WordPress or WP-CLI commands.
Solution
- Run php -v from the command line to verify your PHP version. WordPress typically requires PHP 7.4 or higher.
- If you have control over your hosting environment (e.g., VPS), upgrade your PHP version. For shared hosting, contact your provider to request an upgrade to the necessary PHP version.
- WP-CLI also requires a minimum PHP version. If needed, update WP-CLI using the command wp cli update.
3. Working with WP-CLI When You Don’t Have Root Access
If you are using a shared hosting environment, you may not have root access to install or configure WP-CLI globally. This can limit your ability to use certain WP-CLI features.
Solution
- You can install WP-CLI locally within your WordPress directory by downloading the wp-cli.phar file. To do this:
- Download the WP-CLI PHAR file:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
- Make it executable:
chmod +x wp-cli.phar
- Move it to a directory included in your system’s PATH:
sudo mv wp-cli.phar /usr/local/bin/wp
- If you can’t use sudo, simply run it as php wp-cli.phar from within your WordPress directory.
- If you’re working on a shared host and don’t have root access, create custom shell scripts to automate your WP-CLI tasks. Just make sure you have access to execute PHP scripts and that the correct permissions are set.
Learn more about WP-CLI, the command-line interface for WordPress, and how to streamline your WordPress maintenance workflows!
Advanced Use for Command Line Interface and WordPress
WordPress CLI (WP-CLI) is an incredibly powerful tool for developers who want to streamline their workflow, automate repetitive tasks, and integrate with various systems. Here’s an advanced look at using WP-CLI
- Integration with development workflows: WP-CLI integrates into CI/CD pipelines for automated testing, deployment, and site management, speeding up the development lifecycle.
- Using WP-CLI with version control: Combine WP-CLI with Git to manage theme/plugin updates and configurations, allowing for streamlined version control and collaboration.
- Performance optimizations for large sites: Use WP-CLI to manage large datasets, clear caches, optimize databases, and batch processing tasks, improving speed.
- Scheduled tasks and automation: WP-CLI can automate tasks like backups and updates using cron jobs for recurring maintenance.
- Multisite management commands: WP-CLI simplifies multisite network management by enabling commands for creating, updating, and managing multiple sites at once.