Mỗi lần cập nhật 20 plugin, đổi tên miền hay sao lưu database, bạn lại phải bấm qua hàng loạt trang trong wp-admin? WP-CLI là giao diện dòng lệnh chính thức của WordPress, giúp làm những việc đó bằng một lệnh, và đưa được vào script Bash hoặc cron để chạy tự động. Bài này vừa là hướng dẫn từng bước, vừa là bảng tra cứu lệnh WP-CLI dùng hằng ngày.

Bảng tra nhanh lệnh WP-CLI
| Mục đích | Lệnh |
|---|---|
| Xem danh sách plugin | wp plugin list |
| Cài và kích hoạt plugin | wp plugin install redirection --activate |
| Cập nhật toàn bộ plugin | wp plugin update --all |
| Đổi giao diện | wp theme install colormag --activate |
| Tạo bài viết | wp post create --post_title="..." --post_status=publish |
| Sao lưu database | wp db export |
| Đổi tên miền trong database | wp search-replace 'example.com' 'example.net' |
| Cập nhật lõi WordPress | wp core update |
Yêu cầu
- Máy chủ có user sudo không phải root.
- Bộ LAMP (Linux, Apache, MySQL, PHP) đã cài.
- Một website WordPress đang hoạt động. WP-CLI cũng cài được WordPress mới, nhưng bài này không đề cập phần đó.
WP-CLI cần quyền SSH, nên không dùng được trên shared hosting giới hạn. Nếu bạn muốn toàn quyền dòng lệnh, hãy chạy WordPress trên VPS giá rẻ hoặc VPS Platinum NVMe.
Bước 1: Cài đặt WP-CLI
WP-CLI được đóng gói thành một file Phar (định dạng gói ứng dụng PHP gồm một file duy nhất). Tải về bằng curl:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
Chạy thử để chắc chắn file hoạt động:
php wp-cli.phar --info
OS: Linux 5.4.0-51-generic #56-Ubuntu SMP Mon Oct 5 14:28:49 UTC 2020 x86_64
Shell: /bin/bash
PHP binary: /usr/bin/php7.4
PHP version: 7.4.3
php.ini used: /etc/php/7.4/cli/php.ini
WP-CLI root dir: phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir: phar://wp-cli.phar/vendor
WP_CLI phar path: /home/ayo
WP-CLI packages dir:
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 2.4.0
Cấp quyền thực thi:
chmod +x wp-cli.phar
Chuyển vào /usr/local/bin/ và đổi tên thành wp, để gõ wp từ bất kỳ thư mục nào:
sudo mv wp-cli.phar /usr/local/bin/wp
wp cli version
WP-CLI 2.4.0
Bước 2: Quản lý plugin
Mọi lệnh WP-CLI cần chạy trong thư mục cài WordPress (đổi đường dẫn cho đúng website của bạn):
cd /var/www/wordpress
Liệt kê plugin
wp plugin list
+---------+----------+-----------+---------+
| name | status | update | version |
+---------+----------+-----------+---------+
| akismet | inactive | available | 4.1.7 |
| hello | inactive | none | 1.7.2 |
+---------+----------+-----------+---------+
Cột update hiện available nghĩa là plugin có bản mới.
Tìm plugin trong kho WordPress
wp plugin search seo
Success: Showing 10 of 4278 plugins.
+------------------------------------------------------------+---------------------+--------+
| name | slug | rating |
+------------------------------------------------------------+---------------------+--------+
| Yoast SEO | wordpress-seo | 98 |
| All in One SEO | all-in-one-seo-pack | 92 |
| Rank Math – SEO Plugin for WordPress | seo-by-rank-math | 98 |
| The SEO Framework | autodescription | 98 |
| SEOPress, on-site SEO | wp-seopress | 98 |
| Slim SEO – Fast & Automated WordPress SEO Plugin | slim-seo | 92 |
| W3 Total Cache | w3-total-cache | 88 |
| LiteSpeed Cache | litespeed-cache | 98 |
| SEO 2021 by Squirrly (Smart Strategy) | squirrly-seo | 92 |
| WP-Optimize – Clean, Compress, Cache. | wp-optimize | 96 |
+------------------------------------------------------------+---------------------+--------+
Xem trang kết quả tiếp theo:
wp plugin search seo --page=2
Giá trị ở cột slug là tên bạn dùng trong các lệnh cài và cập nhật.
Cài một hoặc nhiều plugin
wp plugin install jetpack wordpress-seo gutenberg
Installing Jetpack – WP Security, Backup, Speed, & Growth (9.3.1)
Downloading installation package from https://downloads.wordpress.org/plugin/jetpack.9.3.1.zip...
Unpacking the package...
Installing the plugin...
Plugin installed successfully.
Installing Yoast SEO (15.6.2)
Downloading installation package from https://downloads.wordpress.org/plugin/wordpress-seo.15.6.2.zip...
Unpacking the package...
Installing the plugin...
Plugin installed successfully.
Installing Gutenberg (9.8.1)
Downloading installation package from https://downloads.wordpress.org/plugin/gutenberg.9.8.1.zip...
Unpacking the package...
Installing the plugin...
Plugin installed successfully.
Success: Installed 3 of 3 plugins.
Kiểm tra lại bằng wp plugin list:
+---------------+----------+-----------+---------+
| name | status | update | version |
+---------------+----------+-----------+---------+
| akismet | inactive | available | 4.1.7 |
| gutenberg | inactive | none | 9.8.1 |
| hello | inactive | none | 1.7.2 |
| jetpack | inactive | none | 9.3.1 |
| wordpress-seo | inactive | none | 15.6.2 |
+---------------+----------+-----------+---------+
Cài plugin từ file zip bên ngoài (plugin trả phí, plugin tự viết):
wp plugin install https://example.com/wp-content/uploads/myplugin.zip
Cài phiên bản cũ hơn, hữu ích khi bản mới gây lỗi:
wp plugin install jetpack --version=8.0
Kích hoạt và tắt plugin
wp plugin install redirection --activate
wp plugin activate jetpack gutenberg
wp plugin deactivate jetpack gutenberg
wp plugin activate --all
wp plugin deactivate --all
Mẹo cứu site: khi website trắng trang sau khi cập nhật plugin, chạy wp plugin deactivate --all rồi bật lại từng cái để tìm plugin gây lỗi, không cần vào wp-admin.
Cập nhật plugin
wp plugin update akismet
Enabling Maintenance mode...
Downloading update from https://downloads.wordpress.org/plugin/akismet.4.1.8.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Disabling Maintenance mode...
+---------+-------------+-------------+---------+
| name | old_version | new_version | status |
+---------+-------------+-------------+---------+
| akismet | 4.1.7 | 4.1.8 | Updated |
+---------+-------------+-------------+---------+
Success: Updated 1 of 1 plugins.
Thêm --all để cập nhật tất cả cùng lúc.
Xoá plugin
wp plugin delete redirection
Deleted 'redirection' plugin.
Success: Deleted 1 of 1 plugins.
wp plugin delete --all
Bước 3: Quản lý giao diện (theme)
wp theme list
+-----------------+----------+-----------+---------+
| name | status | update | version |
+-----------------+----------+-----------+---------+
| twentynineteen | inactive | available | 1.8 |
| twentytwenty | inactive | none | 1.6 |
| twentytwentyone | active | available | 1.0 |
+-----------------+----------+-----------+---------+
wp theme search color
Success: Showing 10 of 832 themes.
+---------------------+---------------------+--------+
| name | slug | rating |
+---------------------+---------------------+--------+
| Color | color | 0 |
| All Colors | all-colors | 100 |
| Color Blog | color-blog | 98 |
| Color Block | color-block | 0 |
| X Blog color | x-blog-color | 0 |
| Multicolor Business | multicolor-business | 0 |
| ColorNews | colornews | 100 |
| Colorist | colorist | 100 |
| ColorMag | colormag | 98 |
| MultiColors | multicolors | 74 |
+---------------------+---------------------+--------+
Cài và kích hoạt ColorMag:
wp theme install colormag --activate
Installing ColorMag (2.0.4)
Downloading installation package from https://downloads.wordpress.org/theme/colormag.2.0.4.zip...
Unpacking the package...
Installing the theme...
Theme installed successfully.
Activating 'colormag'...
Success: Switched to 'ColorMag' theme.
Success: Installed 1 of 1 themes.
wp theme update --all
Xem thêm tuỳ chọn bằng wp help theme hoặc wp help theme install.

Bước 4: Tạo và quản lý nội dung
wp post list
+----+--------------+-------------+---------------------+-------------+
| ID | post_title | post_name | post_date | post_status |
+----+--------------+-------------+---------------------+-------------+
| 1 | Hello world! | hello-world | 2021-01-24 12:32:06 | publish |
+----+--------------+-------------+---------------------+-------------+
Xoá bài theo ID (bài vào thùng rác):
wp post delete 1
Success: Trashed post 1.
Tạo bài mới và soạn ngay trong trình soạn thảo dòng lệnh. Trong vim: nhấn i để gõ, ESC rồi :wq để lưu.
wp post create --post_status=publish --post_title="Sample post created with WP-CLI" --edit
Success: Created post 6.
Đăng bài từ file văn bản soạn sẵn:
touch content.txt
nano content.txt
wp post create ./content.txt --post_title='Sample Post Created with WP-CLI' --post_status=publish
Tạo trang (page) thay vì bài viết:
wp post create --post_title="A simple page" --post_status=draft --post_type=page
Sinh nội dung giả để thử giao diện
wp post generate
wp post generate --count=20
wp post generate --count=20 --post_type=page
wp help post generate
Dọn revision làm nặng database
Website lâu năm có thể có hàng trăm bản sửa đổi cho mỗi bài, làm database phình to. Xoá vĩnh viễn toàn bộ revision:
wp post delete $(wp post list --post_type='revision' --format=ids) --force
Lệnh con trong $( ) lấy danh sách ID revision, lệnh ngoài xoá chúng với --force (không qua thùng rác). Nên sao lưu database trước khi chạy.
Bước 5: Làm việc với database
wp db cli
wp db query "SELECT user_login,ID FROM wp_users;"
+------------+----+
| user_login | ID |
+------------+----+
| admin | 1 |
+------------+----+
Sao lưu và khôi phục
wp db export
Success: Exported to 'wordpress-2021-01-25-25618e7.sql'.
wp db import file.sql
Có thể đặt wp db export trong cron để sao lưu hằng đêm, rồi đẩy file sang máy chủ khác.
Tìm và thay thế (đổi tên miền)
Luôn chạy thử với --dry-run trước để biết số chỗ sẽ thay:
wp search-replace --dry-run 'example.com' 'example.net'
Success: 10 replacements to be made.
wp search-replace 'example.com' 'example.net'
Khác với sửa trực tiếp bằng SQL, search-replace xử lý đúng dữ liệu serialize của WordPress, nên không làm hỏng widget hay tuỳ chọn theme. Đây là lệnh quan trọng nhất khi chuyển website sang VPS mới hoặc đổi tên miền.
Bước 6: Cập nhật lõi WordPress
wp core version
wp core check-update
wp core update
wp core update --version=5.6
Hạ cấp phiên bản (không khuyến nghị, chỉ dùng khi bắt buộc):
wp core update --version=5.0 --force
Kết luận
WP-CLI rút ngắn đáng kể thời gian quản trị WordPress, từ plugin, theme, nội dung đến database và cập nhật. Dùng wp help <command> để khám phá thêm, và kết hợp với Bash, cron để tự động hoá. Nếu bạn quản lý nhiều website, một Cloud Server có SSH đầy đủ sẽ giúp tận dụng WP-CLI tối đa. Đăng ký tại my.vpshc.com.