Using WP-CLI: Manage WordPress via Command Line
WP-CLI is the command line interface for WordPress. It makes tasks that normally require many clicks in the admin fast and efficient. At Theory7, we have WP-CLI available by default on servers with SSH access. In this guide, you will learn the power of WP-CLI for WordPress management.
Why WP-CLI?
WP-CLI offers significant advantages:
- Speed - Updates in seconds instead of minutes of clicking. This is especially useful for developers and administrators managing multiple sites.
- Automation - Scriptable tasks for multiple sites. This can save time when performing routine maintenance tasks.
- Bulk operations - Update all plugins at once. This is a must for sites with many plugins, as it reduces the chance of incompatibility.
- Access during issues - Works even if wp-admin does not load. This can be crucial during an emergency when the frontend or backend of the site is inaccessible.
- Database operations - Execute SQL queries directly. This provides more control and flexibility in managing your database.
Check WP-CLI Availability
Check if WP-CLI is installed:
wp --version
On Theory7 servers, WP-CLI is available by default. If you cannot find WP-CLI, contact your hosting provider for support.
Basic WP-CLI Usage
Navigating to WordPress
All WP-CLI commands must run from the WordPress root:
cd /var/www/html/wordpress
Or specify the path:
wp --path=/var/www/html/wordpress core version
Check WordPress Version
wp core version
This command shows you the current version of WordPress, which is useful to know before performing updates.
Database Info
wp db cli
This opens the MySQL command line interface, where you can execute SQL commands directly. This can be useful for advanced database operations.
Updating WordPress
Update Core
Check for available updates:
wp core check-update
Perform the update:
wp core update
After the update, update the database:
wp core update-db
Update Plugins
Update all plugins:
wp plugin update --all
Specific plugin:
wp plugin update woocommerce
This is useful if you know that a specific plugin has received an important update.
Update Themes
wp theme update --all
Updating themes is just as important as updating plugins, as outdated themes can also pose security risks.
Plugin Management
View Plugins
wp plugin list
Only active plugins:
wp plugin list --status=active
Install Plugin
wp plugin install wordpress-seo
Install and activate:
wp plugin install wordpress-seo --activate
This is a convenient way to quickly add new functionality to your site.
Activate/Deactivate Plugin
wp plugin activate plugin-name
wp plugin deactivate plugin-name
Delete Plugin
wp plugin delete plugin-name
This is a quick way to remove unused plugins and keep your site tidy.
Theme Management
View Themes
wp theme list
Activate Theme
wp theme activate theme-name
Install Theme
wp theme install flavflavor --activate
It is important to ensure that your themes are up-to-date to maintain compatibility with plugins and security.
User Management
View Users
wp user list
Create New User
wp user create john john@example.com --role=administrator
With password:
wp user create john john@example.com --role=editor --user_pass=password123
Reset Password
wp user update admin --user_pass=newpassword
Delete User
wp user delete 123 --reassign=1
Reassign assigns content to the user with ID 1. This is useful to ensure that no content is lost when deleting a user.
Database Operations
Export Database
wp db export backup.sql
This is crucial for making backups before making major changes.
Import Database
wp db import backup.sql
This can be useful after a migration or if you want to restore a previous version of your database.
Search and Replace
Perfect for migrations:
wp search-replace 'http://old.domain.com' 'https://new.domain.com'
Dry run first:
wp search-replace 'old' 'new' --dry-run
This prevents unwanted changes and helps you see what will change.
Optimize Database
wp db optimize
This helps improve your database performance by freeing up unused space.
Cache Management
Flush Cache
wp cache flush
This can help resolve issues with outdated content still being displayed.
Delete Transients
wp transient delete --all
Transients are temporary data that can be deleted to keep the database clean.
Object Cache
wp cache flush
wp rewrite flush
This can be useful after making changes to your permalink structure.
WordPress Options
View Option
wp option get siteurl
wp option get blogname
Update Option
wp option update blogname "My New Site Title"
Change Site URL
wp option update siteurl 'https://newdomain.com'
wp option update home 'https://newdomain.com'
This is important when moving a site to a new domain.
Maintenance and Repair
Verify Checksums
Check if core files are intact:
wp core verify-checksums
Reinstall Core
wp core download --force
This can be useful if you think files are corrupted or lost.
Repair Database
wp db repair
This can help resolve database issues.
WP-CLI for Multiple Sites
WordPress Multisite
wp site list
wp plugin activate plugin-name --network
This makes managing multiple sites within a network much easier.
Loop Through All Sites
for site in $(wp site list --field=url); do
wp --url=$site plugin update --all
done
This is a powerful way to perform updates for all sites in a multisite installation.
Handy One-Liners
Complete Update
wp core update && wp core update-db && wp plugin update --all && wp theme update --all && wp cache flush
This is a convenient way to update everything at once.
Delete All Inactive Plugins
wp plugin delete $(wp plugin list --status=inactive --field=name)
This helps keep your WordPress installation clean and organized.
Delete Spam Comments
wp comment delete $(wp comment list --status=spam --format=ids)
This can help improve your site's performance by removing unwanted data.
Troubleshooting
Error: MySQL server has gone away
Database connection timeout. Try again or check the database server.
Error: This does not seem to be a WordPress installation
Make sure you are in the WordPress root directory.
Out of memory
wp --memory_limit=512M plugin update --all
This can help if you encounter memory limits while executing commands.
Related Articles
- Server Security Basics
- Server Backup Strategy
- Firewall Configuration (UFW)
- Enable SSH Access in DirectAdmin
More information about WordPress hosting at Theory7
Need Help?
We are here for you! Are you facing any issues or have questions? Our support team is happy to assist you personally. Send us a message via the ticket system - we usually respond within a few hours and are happy to help.
0 van 0 vonden dit nuttig