WP-CLI (WordPress Command Line Interface) is a powerful tool that allows you to manage your WordPress site directly from the command line. It’s especially useful for developers and advanced users who want to perform tasks more efficiently than using the WordPress admin dashboard. In this guide, we’ll introduce you to WP-CLI, explain its benefits, and show you how to get started.
What is WP-CLI?
WP-CLI is a command-line tool for managing WordPress installations. It provides a set of commands that allow you to perform various tasks, such as installing plugins, updating themes, and managing users, without needing to use the WordPress admin dashboard.
Why Use WP-CLI?
- Efficiency:
Perform tasks faster with commands instead of navigating through the admin dashboard. - Automation:
Automate repetitive tasks using scripts. - Remote Management:
Manage your WordPress site from a remote server or local machine. - Developer-Friendly:
Ideal for developers who prefer working in the command line. - No Browser Needed:
Perform tasks without needing a web browser.
How to Install WP-CLI
Step 1: Check System Requirements
- PHP 5.6 or later.
- WordPress 3.7 or later.
- A Unix-like environment (Linux, macOS, or Windows with WSL).
Step 2: Download WP-CLI
- Open Your Terminal:
Access your server via SSH or open a terminal on your local machine. - Download WP-CLI:
Run the following command to download WP-CLI:curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
- Make WP-CLI Executable:
Run the following command to make the downloaded file executable:chmod +x wp-cli.phar
- Move WP-CLI to Your PATH:
Move the file to a directory in your PATH so you can run it from anywhere:sudo mv wp-cli.phar /usr/local/bin/wp
- Verify Installation:
Run the following command to verify that WP-CLI is installed correctly:wp --info
Basic WP-CLI Commands
1. Core Commands
- Install WordPress:
wp core install --url=example.com --title="Example Site" --admin_user=admin --admin_password=password --admin_email=admin@example.com
- Update WordPress:
wp core update
- Check WordPress Version:
wp core version
2. Plugin Commands
- Install a Plugin:
wp plugin install plugin-name --activate
- Update a Plugin:
wp plugin update plugin-name
- List Installed Plugins:
wp plugin list
3. Theme Commands
- Install a Theme:
wp theme install theme-name --activate
- Update a Theme:
wp theme update theme-name
- List Installed Themes:
wp theme list
4. User Commands
- Create a User:
wp user create username email@example.com --role=administrator --user_pass=password
- Update a User:
wp user update username --user_pass=newpassword
- List Users:
wp user list
5. Database Commands
- Export Database:
wp db export backup.sql
- Import Database:
wp db import backup.sql
- Optimize Database:
wp db optimize
Advanced WP-CLI Usage
1. Running Multiple Commands
You can chain multiple commands together using &&
:
wp plugin install plugin-name --activate && wp theme install theme-name --activate
2. Using WP-CLI with Scripts
You can create a shell script to automate tasks:
#!/bin/bash wp core update wp plugin update --all wp theme update --all
Save the script as update.sh
, make it executable, and run it:
chmod +x update.sh ./update.sh
3. Remote Management
You can manage a remote WordPress site by specifying the --ssh
option:
wp --ssh=user@example.com/var/www/example.com plugin list
Best Practices for Using WP-CLI
- Backup Your Site:
Always backup your site before running commands that modify your database or files. - Test Commands:
Test commands on a staging site before running them on your live site. - Use Help:
Use the--help
option to get more information about a command:wp plugin install --help
- Stay Updated:
Regularly update WP-CLI to the latest version to access new features and security updates. - Document Your Commands:
Keep a record of the commands you use for future reference.
Final Thoughts
WP-CLI is a powerful tool that can greatly enhance your WordPress workflow, especially if you’re comfortable working in the command line. By mastering WP-CLI, you can perform tasks more efficiently, automate repetitive processes, and manage your site with greater flexibility.
Have you used WP-CLI before? What commands or scripts do you find most useful? Share your experiences in the comments below! If you have any questions, feel free to ask—we’re here to help!