Chapter 13: WordPress CLI (WP-CLI)
Chapter 13 of 15
Chapter 13: WordPress CLI (WP-CLI)
13.1 WP-CLI Overview
WP-CLI is command-line interface for WordPress.
Benefits:
- Automate WordPress tasks
- Manage sites from command line
- Faster than web interface
- Scriptable operations
13.2 WP-CLI Installation
Install WP-CLI on your server.
# Download WP-CLI
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
# Make executable
chmod +x wp-cli.phar
# Move to PATH
sudo mv wp-cli.phar /usr/local/bin/wp
# Verify installation
wp --info
13.3 Common WP-CLI Commands
Useful WP-CLI commands for daily tasks.
# Core commands
wp core download
wp core install
wp core update
# Plugin commands
wp plugin install plugin-name
wp plugin activate plugin-name
wp plugin list
# Theme commands
wp theme install theme-name
wp theme activate theme-name
wp theme list
# Database commands
wp db export backup.sql
wp db import backup.sql
13.4 Custom WP-CLI Commands
Create custom WP-CLI commands.
// Register custom command
WP_CLI::add_command('my-command', 'my_command_function');
function my_command_function($args, $assoc_args) {
WP_CLI::success('Command executed!');
}
// Usage: wp my-command
13.5 WP-CLI Best Practices
Follow best practices when using WP-CLI.
- Use in development and staging
- Backup before major operations
- Test commands on staging first
- Document custom commands
- Use for automation scripts