在本页面上
- 先决条件
- 入门
- 安装 WP-CLI
- 使用 WP-CLI 管理插件
- 使用 WP-CLI 管理主题
- 使用 WP-CLI 创建和管理文章与页面
- 使用 WP-CLI 管理数据库
- 使用 WP-CLI 更新 WordPress
- 结论
如果你是一名系统管理员,负责管理数百或数千个 WordPress 网站,那么这是一个非常耗时的过程。你将需要登录到每个 WordPress 控制面板,安装或更新插件和主题。这正是 WP-CLI 发挥作用的地方。该
WP-CLI 是一个功能强大的命令行工具,专门设计用于从命令行管理 WordPress。你可以管理多个 WordPress 网站,而无需登录 WordPress 管理面板。使用 WP-CLI,你可以执行多种操作,包括安装和更新插件、主题、创建内容、处理数据库等等。
在本文中,我将展示如何安装和使用 WP-CLI 来管理 WordPress 网站。
先决条件
- 一台运行 Ubuntu 20.04 且已安装 WordPress 的服务器。
- 服务器上已配置 root 密码。
入门
首先,你需要更新系统上的 APT 软件包缓存。你可以使用以下命令来更新它:
apt-get update -y
系统更新完成后,你可以继续下一步。
安装 WP-CLI
首先,使用以下命令下载 WP-CLI 二进制文件:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
下载完成后,为下载的文件设置适当的权限:
chmod +x wp-cli.phar
接下来,使用以下命令将下载的二进制文件复制到系统路径:
cp wp-cli.phar /usr/bin/wp
现在,使用以下命令验证 WP-CLI 版本:
wp cli version --allow-root
你应该会看到以下输出:
WP-CLI 2.5.0
使用 WP-CLI 管理插件
在本节中,我们将学习如何从命令行搜索、安装、更新和删除 WordPress 网站上的插件。
首先,使用以下命令将目录切换到你的 WordPress 网站:
cd /var/www/html/wordpress
要列出 WordPress 网站上安装的所有插件,请运行以下命令:
wp plugin list --allow-root
你应该会看到以下输出:
+---------+----------+--------+---------+ | name | status | update | version | +---------+----------+--------+---------+ | akismet | inactive | none | 4.1.9 | | hello | inactive | none | 1.7.2 | +---------+----------+--------+---------+
要搜索某个特定的插件,请运行以下命令:
wp plugin search cache --allow-root
你应该会在以下输出中看到所有与缓存相关的插件:
Success: Showing 10 of 3688 plugins. +--------------------------------------------------------------------------------+--------------------------+--------+ | name | slug | rating | +--------------------------------------------------------------------------------+--------------------------+--------+ | LiteSpeed Cache | litespeed-cache | 98 | | W3 Total Cache | w3-total-cache | 88 | | WP-Optimize – Cache, Clean, Compress. | wp-optimize | 96 | | WP Fastest Cache | wp-fastest-cache | 98 | | WP Cloudflare Super Page Cache | wp-cloudflare-page-cache | 98 | | Redis Object Cache | redis-cache | 92 | | WP Super Cache | wp-super-cache | 86 | | Autoptimize | autoptimize | 94 | | Hummingbird – Optimize Speed, Enable Cache, Minify CSS & Defer Critical JS | hummingbird-performance | 96 | | Cache Enabler | cache-enabler | 88 | +--------------------------------------------------------------------------------+--------------------------+--------+
现在,使用以下命令从上面的列表中安装该特定插件:
wp plugin install wp-super-cache --allow-root
你应该会看到以下输出:
Installing WP Super Cache (1.7.3) Downloading installation package from https://downloads.wordpress.org/plugin/wp-super-cache.1.7.3.zip... Unpacking the package... Installing the plugin... Plugin installed successfully. Success: Installed 1 of 1 plugins.
现在,使用以下命令确认插件是否已安装:
wp plugin list --allow-root
你应该会看到以下输出:
+----------------+----------+--------+---------+ | name | status | update | version | +----------------+----------+--------+---------+ | akismet | inactive | none | 4.1.9 | | hello | inactive | none | 1.7.2 | | wp-super-cache | inactive | none | 1.7.3 | +----------------+----------+--------+---------+
要使用以下命令从特定来源安装插件:
wp plugin install https://downloads.wordpress.org/plugin/caldera-forms.1.9.4.zip --allow-root
你应该会看到以下输出:
Downloading installation package from https://downloads.wordpress.org/plugin/caldera-forms.1.9.4.zip... Unpacking the package... Installing the plugin... Plugin installed successfully. Success: Installed 1 of 1 plugins.
要激活已安装的插件,请运行以下命令:
wp plugin activate wp-super-cache --allow-root
你应该会看到以下输出:
Plugin 'wp-super-cache' activated. Success: Activated 1 of 1 plugins.
要停用已安装的插件,请运行以下命令:
wp plugin deactivate wp-super-cache --allow-root
你应该会看到以下输出:
Plugin 'wp-super-cache' deactivated. Success: Deactivated 1 of 1 plugins.
要激活所有插件,请运行以下命令:
wp plugin activate --all --allow-root
要更新某个特定的插件,请运行以下命令:
wp plugin update akismet --allow-root
要删除某个特定的插件,请运行以下命令:
wp plugin delete wp-super-cache --allow-root
要删除所有插件,请运行以下命令:
wp plugin delete --all --allow-root
使用 WP-CLI 管理主题
在本节中,我们将向你展示如何使用 WP-CLI 安装、更新、搜索和管理主题。
要列出 WordPress 网站上安装的所有主题,请运行以下命令:
wp theme list --allow-root
你应该会看到以下输出:
+-----------------+----------+--------+---------+ | name | status | update | version | +-----------------+----------+--------+---------+ | twentynineteen | inactive | none | 2.0 | | twentytwenty | inactive | none | 1.7 | | twentytwentyone | active | none | 1.3 | +-----------------+----------+--------+---------+
要搜索某个特定的主题,请运行以下命令:
wp theme search metro --allow-root
你应该会看到所有与 metro 一词匹配的主题:
Success: Showing 4 of 4 themes. +----------------+----------------+--------+ | name | slug | rating | +----------------+----------------+--------+ | Metrolo | metrolo | 100 | | MetroStore | metrostore | 100 | | Metro Magazine | metro-magazine | 98 | | Rara Magazine | rara-magazine | 0 | +----------------+----------------+--------+
要安装并激活 metro 主题,请运行以下命令:
wp theme install metro-magazine --activate --allow-root
你应该会看到以下输出:
Installing Metro Magazine (1.3.5) Downloading installation package from https://downloads.wordpress.org/theme/metro-magazine.1.3.5.zip... Unpacking the package... Installing the theme... Theme installed successfully. Activating 'metro-magazine'... Success: Switched to 'Metro Magazine' theme. Success: Installed 1 of 1 themes.
要更新所有主题,请运行以下命令:
wp theme update --all --allow-root
要删除某个特定的主题,请运行以下命令:
wp theme delete metro-magazine --allow-root
使用 WP-CLI 创建和管理文章与页面
在本节中,我们将向你展示如何使用 WP-CLI 列出、创建和管理文章与页面。
要列出 WordPress 网站上的所有文章,请运行以下命令:
wp post list --allow-root
你应该会得到以下输出:
+----+--------------+-------------+---------------------+-------------+ | ID | post_title | post_name | post_date | post_status | +----+--------------+-------------+---------------------+-------------+ | 1 | Hello world! | hello-world | 2021-06-09 14:51:29 | publish | +----+--------------+-------------+---------------------+-------------+
要删除某个特编号的文章,请运行以下命令:
wp post delete 1 --allow-root
要创建新文章,请运行以下命令:
wp post create --post_status=publish --post_title="How to Manage WordPress with WP-CLI" --edit --allow-root
要创建页面而不是文章,请运行以下命令:
wp post create --post_title="My new page" --post_status=draft --post_type=page --allow-root
要使用虚拟数据生成 30 篇文章,请运行以下命令:
wp post generate --count=30 --allow-root
要列出所有生成的文章,请运行以下命令:
wp post list --allow-root
你应该会看到以下输出:
+----+-------------------------------------+-------------------------------------+---------------------+-------------+ | ID | post_title | post_name | post_date | post_status | +----+-------------------------------------+-------------------------------------+---------------------+-------------+ | 7 | Post 2 | post-2 | 2021-06-09 15:00:57 | publish | | 8 | Post 3 | post-3 | 2021-06-09 15:00:57 | publish | | 9 | Post 4 | post-4 | 2021-06-09 15:00:57 | publish | | 10 | Post 5 | post-5 | 2021-06-09 15:00:57 | publish | | 11 | Post 6 | post-6 | 2021-06-09 15:00:57 | publish | | 12 | Post 7 | post-7 | 2021-06-09 15:00:57 | publish | | 13 | Post 8 | post-8 | 2021-06-09 15:00:57 | publish | | 14 | Post 9 | post-9 | 2021-06-09 15:00:57 | publish | | 15 | Post 10 | post-10 | 2021-06-09 15:00:57 | publish | | 16 | Post 11 | post-11 | 2021-06-09 15:00:57 | publish | | 17 | Post 12 | post-12 | 2021-06-09 15:00:57 | publish | | 18 | Post 13 | post-13 | 2021-06-09 15:00:57 | publish | | 19 | Post 14 | post-14 | 2021-06-09 15:00:57 | publish | | 20 | Post 15 | post-15 | 2021-06-09 15:00:57 | publish | | 21 | Post 16 | post-16 | 2021-06-09 15:00:57 | publish | | 22 | Post 17 | post-17 | 2021-06-09 15:00:57 | publish | | 23 | Post 18 | post-18 | 2021-06-09 15:00:57 | publish | | 24 | Post 19 | post-19 | 2021-06-09 15:00:57 | publish | | 25 | Post 20 | post-20 | 2021-06-09 15:00:57 | publish | | 26 | Post 21 | post-21 | 2021-06-09 15:00:57 | publish | | 27 | Post 22 | post-22 | 2021-06-09 15:00:57 | publish | | 28 | Post 23 | post-23 | 2021-06-09 15:00:57 | publish | | 29 | Post 24 | post-24 | 2021-06-09 15:00:57 | publish | | 30 | Post 25 | post-25 | 2021-06-09 15:00:57 | publish | | 31 | Post 26 | post-26 | 2021-06-09 15:00:57 | publish | | 32 | Post 27 | post-27 | 2021-06-09 15:00:57 | publish | | 33 | Post 28 | post-28 | 2021-06-09 15:00:57 | publish | | 34 | Post 29 | post-29 | 2021-06-09 15:00:57 | publish | | 35 | Post 30 | post-30 | 2021-06-09 15:00:57 | publish | | 36 | Post 31 | post-31 | 2021-06-09 15:00:57 | publish | | 5 | How to Manage WordPress with WP-CLI | how-to-manage-wordpress-with-wp-cli | 2021-06-09 15:00:39 | publish | +----+-------------------------------------+-------------------------------------+---------------------+-------------+
要使用虚拟数据生成页面,请运行以下命令:
wp post generate --count=30 --post_type=page --allow-root
使用 WP-CLI 管理数据库
你还可以使用 WP-CLI 管理数据库。
要备份整个 WordPress 数据库,请运行以下命令:
wp db export --allow-root
你应该会看到以下输出:
Success: Exported to 'mysite-2021-06-09-14d4641.sql'.
你还可以使用以下命令导入 WordPress 数据库:
wp db import backup.sql --allow-root
使用 WP-CLI 更新 WordPress
要打印 WordPress 的当前版本,请运行以下命令:
wp core version --allow-root
你应该会看到以下输出:
5.7.2
要检查 WordPress 更新,请运行以下命令:
wp core check-update --allow-root
你应该会看到以下输出:
Success: WordPress is at the latest version.
现在,你可以使用以下命令将 WordPress 更新到最新的可用版本:
wp core update --allow-root
结论
在上面的指南中,你学习了如何安装和使用 WP-CLI 来管理 WordPress 网站。我希望这将使你的工作更加轻松。