Contents
Introduction to WP-CLI
WP-CLI (https://wp-cli.org/) is the Command-Line Interface for WordPress. It empowers developers and administrators to manage WordPress installations without relying on a web browser. Through simple commands, you can install plugins, update core files, manage users, perform database operations, and automate recurring tasks.
Why Use WP-CLI for Automation
- Speed: Executes tasks in seconds that could take minutes in the dashboard.
- Reproducibility: Same commands yield consistent results across environments.
- Scripting: Embed commands into shell scripts or CI/CD pipelines.
- Scalability: Manage dozens or hundreds of sites programmatically.
Installation and Configuration
1. Prerequisites
- PHP 5.6 or later
- WordPress 3.7 or later
- SSH access (for remote servers)
- Composer (optional, for bundled distribution)
2. Installation Methods
a. Phar Archive
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar php wp-cli.phar --info chmod x wp-cli.phar sudo mv wp-cli.phar /usr/local/bin/wp
b. Composer
composer global require wp-cli/wp-cli export PATH=HOME/.composer/vendor/bin:PATH
3. Verification
After installation, confirm with:
wp --info
Essential WP-CLI Commands
Command | Description |
---|---|
wp core download |
Downloads WordPress core files. |
wp core install |
Installs WordPress with given credentials. |
wp plugin install ltsluggt --activate |
Installs and activates a plugin. |
wp theme update ltthemegt |
Updates a theme to the latest version. |
wp user create |
Creates a new user with specified role. |
Automating Tasks with Shell Scripts
Place repetitive WP-CLI commands into a shell script for one-step execution.
#!/bin/bash # backup-and-update.sh # Navigate to WordPress directory cd /var/www/example.com # Backup database wp db export backups/db_(date %F).sql # Update core, plugins, themes wp core update wp plugin update --all wp theme update --all # Optimize database wp db optimize echo Backup and update complete
Make executable: chmod x backup-and-update.sh
. Run manually or via cron
.
Scheduling with Cron
Edit crontab with crontab -e
and add:
# Daily at 2am: backup and update 0 2 /usr/local/bin/backup-and-update.sh >> /var/log/wp-cli.log 2>1
Advanced Usage and Custom Commands
1. Custom Command Packages
Create a PHP-based WP-CLI package to extend functionality. Define a wp-cli.yml
and a PHP file with annotations:
2. Using Hooks and Aliases
Define aliases to simplify remote workflows in
~/.wp-cli/config.yml
:@production: ssh: user@example.com path: /var/www/site url: https://example.com aliases: prod: @productionThen run:
wp @prod plugin update --all
.Best Practices
- Test in Staging: Always validate scripts on a staging environment first.
- Version Control: Store your shell scripts and custom command packages in Git.
- Logging: Redirect output to log files for audit and troubleshooting.
- Security: Restrict SSH keys, use least-privilege user accounts.
- Backups: Automate database and file backups before updates.
Troubleshooting Common Issues
- “Error: must be run from WordPress root.”
Solution: ensurewp
is executed within the WordPress installation directory or specify--path
.- Permission Denied.
Solution: fix file ownership (chown
) or adjustsudo
rules.- SSH Timeout.
Solution: verify SSH key access, increase--ssh-options=-o ConnectTimeout=30
.Further Reading
Empower your workflow with WP-CLI and automate with confidence.
|
Acepto donaciones de BAT's mediante el navegador Brave 🙂 |