快速入门

祝贺!您已经首次安装了WP-CLI,并准备升级WordPress的使用。本页包含对WP-CLI的简要介绍以及一些示例用法。

介绍

WP-CLI是WordPress的命令行界面。该项目的目标是为WordPress管理员提供一个完整的替代方案;对于您可能希望在WordPress管理中执行的任何操作,应该有一个等效的WP-CLI命令。

例如,因为您可以从WordPress管理员安装插件,所以您也可以使用WP-CLI安装插件

$ wp plugin install akismet
Installing Akismet (3.1.8)
Downloading install package from https://downloads.wordpress.org/plugin/akismet.3.1.8.zip...
Unpacking the package...
Installing the plugin...
Plugin installed successfully.

而且,由于您还可以从WordPress管理员激活插件,因此您可以使用WP-CLI激活插件

$ wp plugin activate akismet
Success: Plugin 'akismet' activated.

使用WordPress管理员和WP-CLI之间的一个关键区别是:执行任何操作所需的点击次数要少得多。随着您对命令行越来越熟悉,您会注意到使用WP-CLI执行给定任务通常比通过WordPress管理员执行相同的任务要快得多。从长远来看,预先投入时间来学习如何更好地使用 WP-CLI 会带来回报。

常用术语

在使用WP-CLI的整个过程中,您会听到一遍又一遍地使用某些术语。

例如,_命令_是WP-CLI功能的原子单元。 就是这样一个命令,就像.命令表示名称(例如“插件安装”)和回调,并注册到 (doc)。wp plugin install``wp plugin activate``WP_CLI::add_command()

_概要_定义命令接受哪些_位置_和_关联_参数。让我们来看看以下的概要:wp plugin install

$ wp plugin install
usage: wp plugin install <plugin|zip|url>... [--version=<version>] [--force] [--activate] [--activate-network]

在此示例中,是公认的_位置_参数。事实上,多次接受相同的位置参数(要安装的插件的 slug、ZIP 或 URL)。 是公认的_关联_论点之一。它用于表示要安装的插件的版本。还要注意参数定义周围的方括号;方括号表示参数是可选的。<plugin|zip|url>...``wp plugin install``[--version=<version>]

WP-CLI还具有一系列_适用于所有_命令的全局参数。例如,包含意味着您的命令执行将显示所有PHP错误,并为WP-CLI引导过程增加额外的冗长程度。--debug

实例

准备好潜入水中了吗?以下是如何使用WP-CLI的一些常见示例:

在几秒钟内下载并安装WordPress

  1. 下载最新版本的WordPress with (doc)。wp core download
$ wp core download --path=wpclidemo.dev
Creating directory '/srv/www/wpclidemo.dev/'.
Downloading WordPress 4.6.1 (en_US)...
Using cached file '/home/vagrant/.wp-cli/cache/core/wordpress-4.6.1-en_US.tar.gz'...
Success: WordPress downloaded.
  1. 创建一个新的 wp-config.php 文件 (doc)。wp config create
$ cd wpclidemo.dev
$ wp config create --dbname=wpclidemo --dbuser=root --prompt=dbpass
1/10 [--dbpass=<dbpass>]:
Success: Generated 'wp-config.php' file.
  1. 基于 wp-config.php with (doc) 创建数据库。wp db create
$ wp db create
Success: Database created.
  1. 安装 WordPress with (doc)。wp core install
$ wp core install --url=wpclidemo.dev --title="WP-CLI" --admin_user=wpcli --admin_password=wpcli --admin_email=info@wp-cli.org
Success: WordPress installed successfully.

就是这样!

将插件更新到最新版本

使用 (doc) 将所有插件更新到最新版本。wp plugin update --all

$ wp plugin update --all
Enabling Maintenance mode...
Downloading update from https://downloads.wordpress.org/plugin/akismet.3.1.11.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Downloading update from https://downloads.wordpress.org/plugin/nginx-champuru.3.2.0.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Disabling Maintenance mode...
Success: Updated 2/2 plugins.
+------------------------+-------------+-------------+---------+
| name                   | old_version | new_version | status  |
+------------------------+-------------+-------------+---------+
| akismet                | 3.1.3       | 3.1.11      | Updated |
| nginx-cache-controller | 3.1.1       | 3.2.0       | Updated |
+------------------------+-------------+-------------+---------+

将用户添加为超级用户

在多站点上,使用 (doc) 向现有用户授予超级用户权限。wp super-admin add

$ wp super-admin add wpcli
Success: Granted super-admin capabilities.

重新生成缩略图

如果您添加或更改了注册到 的图片大小,则可能需要使用 (doc),以便您的主题显示正确的图片尺寸。add_image_size()``wp media regenerate

wp media regenerate --yes
Found 1 image to regenerate.
1/1 Regenerated thumbnails for "charlie-gpa" (ID 4).
Success: Finished regenerating the image.

想知道下一步是什么?浏览所有WP-CLI的命令来探索您的新世界。或者,赶上 shell 朋友了解有用的命令行实用程序。