Say goodbye to tedious clicking operations and experience the speedy management efficiency brought by the command line.
What is WP-CLI?
Imagine being able to do all of your WordPress administrative tasks by typing simple commands in a black command line window: installing plugins, updating themes, creating posts, and even modifying content in bulk - that's the beauty of the WP-CLI (WordPress Command Line Interface).
WP-CLI is the official command line tool for WordPress, which allows you to manage your website through a terminal (Terminal/SSH) without logging into the backend interface. It may sound a bit technically challenged for the uninitiated, but once mastered, you'll find it much more efficient than the traditional graphical interface.
Why should I use WP-CLI?
🚀 10 times more efficient
- batch operation: Update all plugins with one click instead of one click
- automatization: scripts can be written to automate repetitive tasks
- remote management: Manage websites on the server via SSH without downloading files
💻 Real Case Comparison
Update 20 plugins the traditional way.
- Log in to the backend → Click "Plugins" → View updates → Select all plugins → Click Update → Wait for the update to complete
- Time-consuming: It takes 3-5 minutes, and you need to keep clicking and waiting for the page to load.
Use the WP-CLI.
wp plugin update --all Time: 10-20 seconds, one command!
How to install WP-CLI?
Installation on Linux/macOS
Open Terminal and enter the following commands in order:
# 下载 WP-CLI
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
# 赋予执行权限
chmod +x wp-cli.phar
# 移动到系统路径
sudo mv wp-cli.phar /usr/local/bin/wp
# 验证安装
wp --info If you see the version information, the installation was successful!
Installation on Windows
The following two methods are recommended:
- 1.Installing WSL (Windows Subsystem for Linux): Search for Ubuntu in the Microsoft Store and install it.
- 2.Use of integrated environments such as XAMPP: Newer versions usually have WP-CLI built in.
Basic Command Practical Exercise
Let's start with the simplest commands and step through the core functionality of WP-CLI.
1. Basic information check
# 检查 WordPress 版本
wp core version
# 查看网站状态
wp core check-update
# 列出所有已安装的插件
wp plugin list 2. Plug-in management
# 安装一个插件(以 Yoast SEO 为例)
wp plugin install wordpress-seo
# 启用插件
wp plugin activate wordpress-seo
# 停用插件
wp plugin deactivate wordpress-seo
# 更新所有插件
wp plugin update --all
# 批量启用多个插件
wp plugin activate akismet wordpress-seo 3. Theme management
# 安装并启用新主题
wp theme install twentytwentyfour --activate
# 查看当前主题
wp theme status
# 更新所有主题
wp theme update --all 4. Content management
# 创建一篇新文章
wp post create --post_title='我的第一篇文章' --post_content='这是通过命令行创建的内容' --post_status=publish
# 查看文章列表
wp post list
# 批量将文章状态从草稿改为发布
wp post update $(wp post list --post_status=draft --format=ids) --post_status=publish Practical scenarios: solving real problems
Scenario 1: Batch Replacement of Domain Names during Website Migration
When you need to migrate your website from a test domain to an official domain:
# 将数据库中所有 old-domain.com 替换为 new-domain.com
wp search-replace "old-domain.com" "new-domain.com" Versus the traditional way: It is risky to manually export the database, replace it with a text editor, and import it back in.
Scenario 2: Batch Processing Image Optimization
Noticed that all the images in the articles did not have ALT text set:
# 为没有 ALT 文本的图片批量添加描述
wp media list --field=ID | xargs -I % wp media update % --post_title="图片描述" Scenario 3: Quickly Backup Your Website
# 一键备份数据库
wp db export backup-$(date +%Y%m%d).sql
# 备份完成后压缩
gzip backup-$(date +%Y%m%d).sql Advanced Tips: Making Work More Productive
1. Use of aliases to simplify commands
In ~/.bashrcOr ~/.zshrcfile is added:
alias wp-update-all='wp plugin update --all && wp theme update --all'
alias wp-backup='wp db export backup-$(date +%Y%m%d).sql' then execute source ~/.bashrc, after which you can use the short command:
wp-update-all # 更新所有插件和主题
wp-backup # 快速备份 2. Creating automation scripts
New file daily-maintenance.sh:
#!/bin/bash
# 每日维护脚本
echo "开始 WordPress 日常维护..."
# 备份数据库
wp db export backup-$(date +%Y%m%d).sql
# 更新所有插件和主题
wp plugin update --all
wp theme update --all
# 清理修订版
wp post delete $(wp post list --post_type=revision --format=ids) --force
echo "维护完成!" Give script execution permissions:chmod +x daily-maintenance.sh
It can then be set up as a timed task to execute automatically.
Frequently Asked Questions (FAQ)
❓ I'm purely new to this, should I use WP-CLI?
suggestion: If you only update a few articles occasionally, you can continue to use the GUI. But if you manage multiple websites or need to operate them frequently, learning WP-CLI is highly recommended.
❓ Will using WP-CLI screw up my website?
safety: WP-CLI has the same privilege control as the GUI. As long as the operation is done carefully (especially the database related commands), the risk is very small. Backup is recommended before operation.
❓ What if I can't remember a command?
skill: Use wp helpCheck out help such as wp help pluginView plugin related commands. Use the Tab key more often for auto-completion.
❓ Do all hosts support WP-CLI?
request: Supported by most VPS and cloud hosts. Shared hosting requires contacting customer service to confirm SSH access.
Suggested study routes
Phase 1 (Week 1): Familiarize yourself with basic commands
- -Practice plugin and theme installations and updates
- -Attempts to create and edit articles
- -Mastery
wp --helpWays to view help
Phase II (weeks 2-3): Practical Application
- -Attempts to replace domain names in bulk
- -Setting up automatic backup scripts
- -Batch processing of learning content
Phase 3 (after 1 month): Advanced Optimization
- -Integration into workflow
- -Writing customized scripts
- -Explore advanced features (e.g. CRON tasks)
do
- Official Resources
- Tutorials & Guides
- Community & Support
summarize
WP-CLI is not an exclusive tool for hackers, but an efficiency tool that every WordPress user should master. Starting today, learn one command a day, and in a month you'll see a quantum leap in your productivity.
Don't be intimidated by the command line-- it is just another language to communicate with computers. Like learning a foreign language, it can be a little difficult at first, but once mastered, it opens the door to a new world.
Now open a terminal and type in your first WP-CLI command!
wp --info Congratulations, you've taken the first step to becoming a WordPress master! 🎉
draw attention to sth.
Tip: All commands in this article are executed in the root directory of the WordPress installation. If you encounter permission problems, you can prefix the commands with sudo(Linux/macOS) or run as administrator (Windows).