
Cài đặt WP CLI và quản lý WordPress bằng câu lệnh WP CLI
Sơ đồ trang
- WP-CLI là gì?
- Cài đặt WP-CLI
- Cách sử dụng WP-CLI
- Một số câu lệnh thông dụng với WP-CLI
- 1. Cài mới 1 plugin bằng wp cli
- 2. List toàn bộ plugin đang sử dụng cho website
- 3. Check phiên bản WordPress bạn đang sử dụng
- 4. Cập nhật wp-core cho website
- 5. Check sum WordPress core
- 6. Xóa toàn bộ transient
- 7. Export database (sao lưu database) bằng wp cli
- 8. Import database (khôi phục database) bằng wp cli
- 8. Tối ưu database bằng wp cli
- 9. Sửa lỗi WordPress bằng wp cli
- 10. Cập nhật SITE URLs bằng WP CLI
- 11. Lấy danh sách tất cả user trong website
- 12. Tạo user mới với quyền quản trị viên
- 13. Lấy thông tin các theme đang sử dụng
- 14. Cài đặt theme mới
- 15. Xóa cache với wp cli
- 16. Regenerate Thumbnails
- 17. Tìm kiếm và thay thế
- 18. Update lại permalink
- 19. Cập nhật lại permalink
- 20. WP CLI cho plugin Litespeed và WP-Rocket
- 21. WP-CLI Database Functions Cheat Sheet
- Kết luận
Ngoài quản lý WordPress qua giao diện trực quan thì bạn có thể quản lý WordPress bằng cách sử dụng các câu lệnh WP CLI , câu lệnh WP CLI sẽ rất hữu ích cho lập trình viên xử lý các sự cố khi không thể hoặc không muốn đăng nhập vào wp-admin thông qua giao diện bình thường.
Việc quản lý website WordPress thông qua câu lệnh cũng khá đơn giản mà chỉ cần vài phút làm quen bạn có thể hiểu và thao tác được, các thao tác cài đặt WP CLI và sử dụng bạn có thể tham khảo bài viết dưới đây.
WP-CLI là gì?
WP-CLI là tính năng được ra đời và phát triển từ những năm 2011 , với WP-CLI bạn có thể dễ dàng thao tác quản lý website WordPress của mình một cách đơn giản thông qua giao diện dòng lệnh (Command).
Với cách thức thông thường , khi các bạn cần cài đặt Plugins, Themes, hay chỉnh sửa liên quan đến WordPress các bạn cần phải truy cập vào trình quản trị WordPress và thực hiện các thao tác bằng cách click chuột nhưng với WP-CLI bạn chỉ cần gõ các dòng lệnh để thực hiện.
Cài đặt WP-CLI
Mặc định WP-CLI sẽ không được cài đặt mà bạn phải cài đặt thủ công vào máy chủ của bạn , nếu bạn sử dụng các dịch vụ Server/VPS nhưng chưa được cài đặt sẵn WP-CLI thì có thể thực hiện các bước sau đây để cài đặt.
1. Bạn cần đăng nhập vào SSH vào VPS của mình với quyền root
2. Sau khi đã SSH thành công các bạn cần chạy các lệnh sau:
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
Chúc thích lệnh:
1. Tải WP-CLI từ Github
2. Xác nhận tải thành công.
3. Phân quyền thực thi fille wp-cli.phar
4. Di chuyển WP-CLI đến phân vùng mà tất cả người dùng có thể sử dụng lệnh và đổi tên từ wp-cli.phar
thành wp
Lý do bạn cần đổi wp-cli.phar
thành wp
là để trong quá trình sử dụng lệnh, bạn chỉ cần gõ wp
thay vì wp-cli.phar
Cách sử dụng WP-CLI
Khi bạn cài đặt WP-CLI lên các máy chủ server/VPS và đang thực hiện thao tác với quyền root, thì khi sử dụng các lệnh của WP-CLI các bạn cần thêm vào option
--allow-root
phía sau dòng lệnh nếu không các bạn sẽ nhận được cảnh báo lỗi như phía dưới. Nếu các bạn đang sử dụng các dịch vụ Sharehost hoặc không phải thao thác từ user root thì bạn không cần phải thêm option này vào phía sau.
Error: YIKES! It looks like you're running this as root. You probably meant to run this as the user that your WordPress installation exists under. If you REALLY mean to run this as root, we won't stop you, but just bear in mind that any code on this site will then have full control of your server, making it quite DANGEROUS. If you'd like to continue as root, please run this again, adding this flag: --allow-root If you'd like to run it as the user that this site is under, you can run the following to become the respective user: sudo -u USER -i -- wp
Bạn phải di chuyển tới thư mục gốc chưa website thì mới thao tác được lệnh WP-CLI nếu ở ngoài thư mục gốc chứa website WordPress thì sẽ nhận được thông báo như bên dưới. Trước khi thao tác với WP-CLI bạn phải dùng lệnh
cd /thu-muc-goc-chua-website-wp/
[root@vutruso ~]# wp --info --allow-root OS: Linux 4.18.0-305.19.1.el8_4.x86_64 #1 SMP Wed Sep 15 15:39:39 UTC 2021 x86_64 Shell: /bin/bash PHP binary: /usr/local/lsws/lsphp74/bin/php PHP version: 7.4.22 php.ini used: /usr/local/lsws/lsphp74/etc/php.ini MySQL binary: /usr/bin/mysql MySQL version: mysql Ver 15.1 Distrib 10.3.28-MariaDB, for Linux (x86_64) using readline 5.1 SQL modes: STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION 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: /root WP-CLI packages dir: WP-CLI global config: WP-CLI project config: WP-CLI version: 2.5.0
Lệnh kiểm tra phiên bản WP-CLI.
wp --info
wp --info --allow-root OS: Linux 4.18.0-305.19.1.el8_4.x86_64 #1 SMP Wed Sep 15 15:39:39 UTC 2021 x86_64 Shell: /bin/bash PHP binary: /usr/local/lsws/lsphp74/bin/php PHP version: 7.4.22 php.ini used: /usr/local/lsws/lsphp74/etc/php.ini MySQL binary: /usr/bin/mysql MySQL version: mysql Ver 15.1 Distrib 10.3.28-MariaDB, for Linux (x86_64) using readline 5.1 SQL modes: 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/bitches.vn/public_html WP-CLI packages dir: WP-CLI global config: WP-CLI project config: WP-CLI version: 2.5.0 [root@vutruso public_html]
Lệnh kiểm tra danh sách các tính năng được hỗ trợ qua WP-CLI.
wp help
Như hình bên dưới các bạn có thể thấy WP-CLI hỗ trợ rất nhiều tính năng liên quan đến WordPress, để sử dụng tính năng nào các bạn chỉ việc gõ lệnh tương ứng: wp
NAME wp DESCRIPTION Manage WordPress through the command-line. SYNOPSIS wp SUBCOMMANDS cache Adds, removes, fetches, and flushes the WP Object Cache object. cap Adds, removes, and lists capabilities of a user role. cli Reviews current WP-CLI info, checks for updates, or views defined aliases. comment Creates, updates, deletes, and moderates comments. config Generates and reads the wp-config.php file. core Downloads, installs, updates, and manages a WordPress installation. cron Tests, runs, and deletes WP-Cron events; manages WP-Cron schedules. db Performs basic database operations using credentials stored in wp-config.php. embed Inspects oEmbed providers, clears embed cache, and more. eval Executes arbitrary PHP code. eval-file Loads and executes a PHP file. export Exports WordPress content to a WXR file. help Gets help on WP-CLI, or on a specific command. i18n Provides internationalization tools for WordPress projects. import Imports content from a given WXR file. language Installs, activates, and manages language packs. maintenance-mode Activates, deactivates or checks the status of the maintenance mode of a site. media Imports files as attachments, regenerates thumbnails, or lists registered image sizes. menu Lists, creates, assigns, and deletes the active theme's navigation menus. network Perform network-wide operations. option Retrieves and sets site options, including plugin and WordPress settings. package Lists, installs, and removes WP-CLI packages. plugin Manages plugins, including installs, activations, and updates. post Manages posts, content, and meta. post-type Retrieves details on the site's registered post types. rewrite Lists or flushes the site's rewrite rules, updates the permalink structure. role Manages user roles, including creating new roles and resetting to defaults. scaffold Generates code for post types, taxonomies, plugins, child themes, etc. search-replace Searches/replaces strings in the database. server Launches PHP's built-in web server for a specific WordPress installation. shell Opens an interactive PHP console for running and testing PHP code. sidebar Lists registered sidebars. site Creates, deletes, empties, moderates, and lists one or more sites on a multisite installation. super-admin Lists, adds, or removes super admin users on a multisite installation. taxonomy Retrieves information about registered taxonomies. term Manages taxonomy terms and term meta, with create, delete, and list commands. theme Manages themes, including installs, activations, and updates. transient Adds, gets, and deletes entries in the WordPress Transient Cache. user Manages users, along with their roles, capabilities, and meta. widget Manages widgets, including adding and moving them within sidebars.
Ví dụ bạn cần xem chi tiết các lệnh liên quan đến Plugin , trước hết các bạn nhấn phím Q để thoát khỏi giao diện trên và sử dụng lệnh tương ứng như sau:
wp plugin
Tại đây bạn sẽ được liệt kê các lệnh cụ thể được hỗ trợ với Plugin như: active, deactivate, delete, uninstall plugin..vv
[root@vutruso public_html]# wp plugin --allow-root usage: wp plugin activate [<plugin>...] [--all] [--network] or: wp plugin auto-updates <command> or: wp plugin deactivate [<plugin>...] [--uninstall] [--all] [--network] or: wp plugin delete [<plugin>...] [--all] or: wp plugin get <plugin> [--field=<field>] [--fields=<fields>] [--format=<format>] or: wp plugin install <plugin|zip|url>... [--version=<version>] [--force] [--activate] [--activate-network] [--insecure] or: wp plugin is-active <plugin> [--network] or: wp plugin is-installed <plugin> or: wp plugin list [--<field>=<value>] [--field=<field>] [--fields=<fields>] [--format=<format>] [--status=<status>] or: wp plugin path [<plugin>] [--dir] or: wp plugin search <search> [--page=<page>] [--per-page=<per-page>] [--field=<field>] [--fields=<fields>] [--format=<format>] or: wp plugin status [<plugin>] or: wp plugin toggle <plugin>... [--network] or: wp plugin uninstall [<plugin>...] [--deactivate] [--skip-delete] [--all] or: wp plugin update [<plugin>...] [--all] [--exclude=<name>] [--minor] [--patch] [--format=<format>] [--version=<version>] [--dry-run] [--insecure] or: wp plugin verify-checksums [<plugin>...] [--all] [--strict] [--format=<format>] [--insecure] See 'wp help plugin <command>' for more information on a specific command.
Một số câu lệnh thông dụng với WP-CLI
1. Cài mới 1 plugin bằng wp cli
Ví dụ bạn muốn cài đặt mới 1 plugin là wordpress-seo qua WP-CLI bạn có thể sử dụng lệnh (tên plugin là slug plugin: https://vi.wordpress.org/plugins/wordpress-seo
/
wp plugin install wordpress-seo --allow-root
Sau khi cài xong bạn có thể active bằng lệnh:
wp plugin activate wordpress-seo --allow-root
Cài xong bạn ghét quá muốn deactive thì bạn dùng lệnh
wp plugin deactivate wordpress-seo --allow-root
Muốn xóa hẳn Yoast SEO bạn sử dụng câu lệnh
wp plugin uninstall wordpress-seo --allow-root
Ngoài ra một số câu lệnh khác mà thường dùng như:
2. List toàn bộ plugin đang sử dụng cho website
wp plugin list --allow-root
Cập nhật toàn bộ plugin lên phiên bản mới nhất
wp plugin update --all
Hủy kích hoạt toàn bộ plugin
wp plugin deactivate --all
3. Check phiên bản WordPress bạn đang sử dụng
wp core version --allow-root
4. Cập nhật wp-core cho website
wp core update --allow-root
5. Check sum WordPress core
wp checksum core --allow-root
[root@vutruso public_html]# wp checksum core --allow-root Success: WordPress installation verifies against checksums.
6. Xóa toàn bộ transient
wp transient delete –all --allow-root
7. Export database (sao lưu database) bằng wp cli
wp db export --allow-root
8. Import database (khôi phục database) bằng wp cli
wp db import <file>
8. Tối ưu database bằng wp cli
wp db optimize --allow-root
9. Sửa lỗi WordPress bằng wp cli
wp db repair --allow-root
10. Cập nhật SITE URLs bằng WP CLI
wp option update home https://vutruso.com wp option update siteurl https://vutruso.com
11. Lấy danh sách tất cả user trong website
wp user list --allow-root
12. Tạo user mới với quyền quản trị viên
wp user create VTS quantri@vutruso.com --role=administrator --allow-root
Cập nhật thông tin tài khoản
wp user update vts --display_name=VUTRUSO--user_pass=vutruso_123pass --allow-root
Xóa thành viên
wp user delete vts --reassign=vts --allow-root
Thay đổi mật khẩu user
wp user update VUTRUSO --user_pass="NEW_PASSWORD" --allow-root
13. Lấy thông tin các theme đang sử dụng
wp theme list --allow-root
14. Cài đặt theme mới
wp theme install twentynineteen --allow-root
Active theme mới cài: wp theme activate twentynineteen --allow-root
15. Xóa cache với wp cli
wp-cli có thể thao tác với object-cache mà không yêu cầu bất kỳ plugin riêng biệt nào.
Để nhanh chóng xóa bộ object-cache banjc so thể dùng lệnh
wp cache flush
16. Regenerate Thumbnails
wp media regenerate --yes --only-missing
17. Tìm kiếm và thay thế
wp search-replace 'https://old.blog' 'https://new.blog' --skip-columns=guid
18. Update lại permalink
wp rewrite flush
19. Cập nhật lại permalink
Cập nhật url tại đường dẫn tĩnh () trong WordPress bằng lệnh dưới đây.
https://vutruso.com/wp-admin/options-permalink.php
wp option update permalink_structure /%postname%/
wp rewrite structure /%postname%/
20. WP CLI cho plugin Litespeed và WP-Rocket
Ngoài ra nếu bạn sử dụng Litespeed cache và muốn sử dụng các câu lệnh để quản lý có thể tham khảo tại: https://docs.litespeedtech.com/lscache/lscwp/cli/
Hoặc wp cli cho WP-Rocket: https://docs.wp-rocket.me/article/1497-wp-cli-interface-for-wp-rocket
21. WP-CLI Database Functions Cheat Sheet
Kết luận
Ở trên là những kiến thức cơ bản nhất để bạn quản lý WordPress thông qua WP-CLI , ngoài một số thứ mình nêu trong bài viết bạn có thể đọc tài liệu trên Github để có thể hiểu và vận dụng các câu lệnh trong việc quản lý WordPress được dễ dàng và thuận tiện nhất.
Chúc bạn thành công.