|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: WordPress 的命令行工具 |
| 4 | +--- |
| 5 | + |
| 6 | +[WP-CLI](https://wp-cli.org/) 是一款用于管理 [WordPress](https://wordpress.org/) 的命令行工具,无需浏览器即可完成插件更新和多站点设置等许多操作。 |
| 7 | + |
| 8 | +可以通过[以下方式](https://make.wordpress.org/cli/2019/06/27/thanks-to-the-2019-sponsors/)进行持续维护: |
| 9 | + |
| 10 | +<a href="https://automattic.com/"><img src="https://make.wordpress.org/cli/files/2017/04/automattic-1.png" style="width:19%;height:auto;display:inline-block;vertical-align:middle;" alt="" width="160" height="35" class="aligncenter size-full wp-image-347" /></a> <a href="https://www.bluehost.com/"><img class="aligncenter size-full wp-image-335" style="width:19%;height:auto;display:inline-block;vertical-align:middle;" src="https://make.wordpress.org/cli/files/2017/04/bluehost.png" alt="" width="160" height="26" /></a> <a href="https://pantheon.io/"><img class="aligncenter size-full wp-image-333" style="width:19%;height:auto;display:inline-block;vertical-align:middle;" src="https://make.wordpress.org/cli/files/2019/06/pantheon.png" alt="" width="160" height="50" /></a> <a href="https://www.siteground.com/"><img class="aligncenter size-full wp-image-332" style="width:19%;height:auto;display:inline-block;vertical-align:middle;" src="https://make.wordpress.org/cli/files/2019/06/SG_logo.png" alt="" width="160" height="33" /></a> <a href="https://wpengine.com/"><img class="aligncenter size-full wp-image-333" style="width:19%;height:auto;display:inline-block;vertical-align:middle;" src="https://make.wordpress.org/cli/files/2017/04/wpengine.png" alt="" width="160" height="30" /></a> |
| 11 | + |
| 12 | +目前的稳定版本是 [2.4.0](https://make.wordpress.org/cli/2019/11/12/wp-cli-v2-4-0-release-notes/)。如果您想获取最新信息,请在 Twitter 上关注 [@wpcli](https://twitter.com/wpcli) 或者 [订阅邮件通知](https://make.wordpress.org/cli/subscribe/)。请参阅 [产品路线图](https://make.wordpress.org/cli/handbook/roadmap/) 了解未来更新规划。 |
| 13 | + |
| 14 | +[](https://travis-ci.org/wp-cli/wp-cli) [](https://isitmaintained.com/project/wp-cli/wp-cli "Average time to resolve an issue") [](https://isitmaintained.com/project/wp-cli/wp-cli "Percentage of issues still open") |
| 15 | + |
| 16 | +导航链接:[使用](#使用方法) | [安装](#安装说明) | [支持](#技术支持) | [扩展](#扩展) | [贡献](#贡献) | [参考](#参考) |
| 17 | + |
| 18 | +## 使用方法 |
| 19 | + |
| 20 | +WP-CLI 可以为您在 WordPress 后台管理中执行的许多操作提供命令行工具。例如,使用 `wp plugin install --activate`([说明文档](https://developer.wordpress.org/cli/commands/plugin/install/))安装并激活插件: |
| 21 | + |
| 22 | +```bash |
| 23 | +$ wp plugin install user-switching --activate |
| 24 | +Installing User Switching (1.0.9) |
| 25 | +Downloading installation package from https://downloads.wordpress.org/plugin/user-switching.1.0.9.zip... |
| 26 | +Unpacking the package... |
| 27 | +Installing the plugin... |
| 28 | +Plugin installed successfully. |
| 29 | +Activating 'user-switching'... |
| 30 | +Plugin 'user-switching' activated. |
| 31 | +Success: Installed 1 of 1 plugins. |
| 32 | +``` |
| 33 | + |
| 34 | +WP-CLI 还包含许多您无法在 WordPress 后台管理中执行的操作命令。例如,`wp transient delete --all`([说明文档](https://developer.wordpress.org/cli/commands/transient/delete/))可以删除一个或所有的 Transients : |
| 35 | + |
| 36 | +```bash |
| 37 | +$ wp transient delete --all |
| 38 | +Success: 34 transients deleted from the database. |
| 39 | +``` |
| 40 | + |
| 41 | +有关如何使用 WP-CLI 的更多内容请阅读《[Quick Start](https://make.wordpress.org/cli/handbook/quick-start/)》。您也可以在 [Shell Friends](https://make.wordpress.org/cli/handbook/shell-friends/) 了解实用的命令行工具。 |
| 42 | + |
| 43 | +如果您已经了解这些基础知识,可以到 [WP-CLI Commands](https://developer.wordpress.org/cli/commands/) 了解更多有关主题和插件管理、数据导入和导出、数据库操作的内容。 |
| 44 | + |
| 45 | +## 安装说明 |
| 46 | + |
| 47 | +下载 Phar 文件是我们推荐的安装方法,如果需要[其他安装方法](https://make.wordpress.org/cli/handbook/installing/)([Composer](https://make.wordpress.org/cli/handbook/installing/#installing-via-composer), [Homebrew](https://make.wordpress.org/cli/handbook/installing/#installing-via-homebrew), [Docker](https://make.wordpress.org/cli/handbook/installing/#installing-via-docker)),请参阅相关文档。 |
| 48 | + |
| 49 | +在安装 WP-CLI 之前,请确保您的操作环境满足最低要求: |
| 50 | + |
| 51 | +- UNIX 环境(OS X,Linux,FreeBSD,Cygwin),某些功能在 Windows 中将受到限制。 |
| 52 | +- PHP 5.4 或更高版本。 |
| 53 | +- WordPress 3.7 或更高版本,较旧的版本在功能上可能会有所减少。 |
| 54 | + |
| 55 | +再次检查操作环境,使用 `wget` 或 `curl` 下载 [wp-cli.phar](https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar): |
| 56 | + |
| 57 | +```bash |
| 58 | +curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar |
| 59 | +``` |
| 60 | + |
| 61 | +接下来,检查 Phar 文件确保其正常运行: |
| 62 | + |
| 63 | +```bash |
| 64 | +php wp-cli.phar --info |
| 65 | +``` |
| 66 | + |
| 67 | +要使用 `wp` 执行 WP-CLI 命令,必须有执行权限并且 `PATH` 已在环境变量中注册,例如: |
| 68 | + |
| 69 | +```bash |
| 70 | +chmod +x wp-cli.phar |
| 71 | +sudo mv wp-cli.phar /usr/local/bin/wp |
| 72 | +``` |
| 73 | + |
| 74 | +如果 WP-CLI 安装成功,使用 `wp --info` 命令应该看到以下输出信息: |
| 75 | + |
| 76 | +```bash |
| 77 | +$ wp --info |
| 78 | +OS: Darwin 16.7.0 Darwin Kernel Version 16.7.0: Thu Jan 11 22:59:40 PST 2018; root:xnu-3789.73.8~1/RELEASE_X86_64 x86_64 |
| 79 | +Shell: /bin/zsh |
| 80 | +PHP binary: /usr/local/bin/php |
| 81 | +PHP version: 7.0.22 |
| 82 | +php.ini used: /etc/local/etc/php/7.0/php.ini |
| 83 | +WP-CLI root dir: /home/wp-cli/.wp-cli/vendor/wp-cli/wp-cli |
| 84 | +WP-CLI vendor dir: /home/wp-cli/.wp-cli/vendor |
| 85 | +WP-CLI packages dir: /home/wp-cli/.wp-cli/packages/ |
| 86 | +WP-CLI global config: /home/wp-cli/.wp-cli/config.yml |
| 87 | +WP-CLI project config: |
| 88 | +WP-CLI version: 2.4.0 |
| 89 | +``` |
| 90 | + |
| 91 | +### 升级 WP-CLI |
| 92 | + |
| 93 | +如果需要更新 WP-CLI ,可以运行 `wp cli update`([说明文档](https://developer.wordpress.org/cli/commands/cli/update/))或者重复上述安装方法。 |
| 94 | + |
| 95 | +如果 WP-CLI 的所有者是 root 或其他系统管理员,则需要执行 `sudo wp cli update` 操作。 |
| 96 | + |
| 97 | +WordPress 的发展相当迅速,Nightly Builds 版本可以在开发环境中使用,并且始终包含最新和最出色的 WP-CLI 功能,运行 `wp cli update --nightly` 即可使用。 |
| 98 | + |
| 99 | +### Tab 命令行补全 |
| 100 | + |
| 101 | +WP-CLI 带有用于 Bash 和 ZSH 的命令行补全脚本。下载 [wp-completion.bash](https://raw.githubusercontent.com/wp-cli/wp-cli/v2.4.0/utils/wp-completion.bash) 并 `~/.bash_profile` 加载它即可。例如: |
| 102 | + |
| 103 | +```bash |
| 104 | +source /FULL/PATH/TO/wp-completion.bash |
| 105 | +``` |
| 106 | + |
| 107 | +在此之后不要忘记运行 `source ~/.bash_profile` 。 |
| 108 | + |
| 109 | +对于 ZSH,您可能需要在加载 `bashcompinit` 后加载 `wp-completion.bash`,将以下内容放入您的 `.zshrc` 中: |
| 110 | + |
| 111 | +```bash |
| 112 | +autoload bashcompinit |
| 113 | +bashcompinit |
| 114 | +source /FULL/PATH/TO/wp-completion.bash |
| 115 | +``` |
| 116 | + |
| 117 | +## 技术支持 |
| 118 | + |
| 119 | +WP-CLI 的维护者和贡献者只有有限的时间回答常见问题,只有[最新版](https://make.wordpress.org/cli/handbook/roadmap/)的 WP-CLI 受到官方支持。 |
| 120 | + |
| 121 | +在寻求帮助时,请首先在下面资源中搜索您的问题: |
| 122 | + |
| 123 | +* [Common issues and their fixes](https://make.wordpress.org/cli/handbook/common-issues/) |
| 124 | +* [WP-CLI handbook](https://make.wordpress.org/cli/handbook/) |
| 125 | +* [Open or closed issues in the WP-CLI GitHub organization](https://github.com/issues?utf8=%E2%9C%93&q=sort%3Aupdated-desc+org%3Awp-cli+is%3Aissue) |
| 126 | +* [Threads tagged 'WP-CLI' in the WordPress.org support forum](https://wordpress.org/support/topic-tag/wp-cli/) |
| 127 | +* [Questions tagged 'WP-CLI' in the WordPress StackExchange](https://wordpress.stackexchange.com/questions/tagged/wp-cli) |
| 128 | + |
| 129 | +如果上面任何一种方式都找不到答案: |
| 130 | + |
| 131 | +* 加入 [WordPress.org 的 Slack](https://make.wordpress.org/chat/) 中的 `#cli` 频道,与当时可能有空的人聊天(这是最快的方法)。 |
| 132 | +* 在 [WordPress 支持论坛](https://wordpress.org/support/forum/wp-advanced/#new-post) 上发布新帖子,并用 「wp-cli」标记它(以便被找到)。 |
| 133 | + |
| 134 | +GitHub Issues 用于跟踪现有命令的改进和 BUG,而不是常规支持。在提交 BUG 报告之前,请务必阅读手册中的 [Bug Reports](https://make.wordpress.org/cli/handbook/bug-reports/),以确保及时解决您的问题。 |
| 135 | + |
| 136 | +请不要在 Twitter 上提出问题,因为: |
| 137 | + |
| 138 | +1)一般很难用 280 个字符的对话解决问题; |
| 139 | + |
| 140 | +2)不能在其他人的对话中找到相同问题的答案。 |
| 141 | + |
| 142 | +开源许可证授予您使用和修改的权利,但不授予您浪费别人时间的权利,请尊重他人! |
| 143 | + |
| 144 | +## 扩展 |
| 145 | + |
| 146 | +每个**命令**都被定义为一个 WP-CLI 功能,`wp plugin install`([说明文档](https://developer.wordpress.org/cli/commands/plugin/install/))是一个,而 `wp plugin activate`( [说明文档](https://developer.wordpress.org/cli/commands/plugin/activate/))是另一个。 |
| 147 | + |
| 148 | +在 WP-CLI 中,各种可执行类、函数和闭包都可以作为命令执行。在 PHPdoc 中描述了作为命令执行所需的信息。`WP_CLI::add_command()` ([说明文档](https://make.wordpress.org/cli/handbook/internal-api/wp-cli-add-command/)) 用于注册内部命令和第三方命令。 |
| 149 | + |
| 150 | +```php |
| 151 | +/** |
| 152 | + * Delete an option from the database. |
| 153 | + * |
| 154 | + * Returns an error if the option didn't exist. |
| 155 | + * |
| 156 | + * ## OPTIONS |
| 157 | + * |
| 158 | + * <key> |
| 159 | + * : Key for the option. |
| 160 | + * |
| 161 | + * ## EXAMPLES |
| 162 | + * |
| 163 | + * $ wp option delete my_option |
| 164 | + * Success: Deleted 'my_option' option. |
| 165 | + */ |
| 166 | +$delete_option_cmd = function( $args ) { |
| 167 | + list( $key ) = $args; |
| 168 | + |
| 169 | + if ( ! delete_option( $key ) ) { |
| 170 | + WP_CLI::error( "Could not delete '$key' option. Does it exist?" ); |
| 171 | + } else { |
| 172 | + WP_CLI::success( "Deleted '$key' option." ); |
| 173 | + } |
| 174 | +}; |
| 175 | +WP_CLI::add_command( 'option delete', $delete_option_cmd ); |
| 176 | +``` |
| 177 | + |
| 178 | +创建自定义 WP-CLI 命令看起来比较容易。阅读 [Commands Cookbook](https://make.wordpress.org/cli/handbook/commands-cookbook/) 了解更多内容,浏览 [Internal API](https://make.wordpress.org/cli/handbook/internal-api/) 发现更多较为实用的功能。 |
| 179 | + |
| 180 | +## 贡献 |
| 181 | + |
| 182 | +感谢您主动为 WP-CLI 做出贡献!正因为您和社区的支持,才能使 WP-CLI 更加出色! |
| 183 | + |
| 184 | +**贡献不仅限于代码**,我们鼓励您在能力范围内作出贡献。比如编写教程、在会议上进行演示、帮助其他用户解决他们的问题,或者协助我们修改文档。 |
| 185 | + |
| 186 | +如果要参与该项目,请仔细阅读手册中的 [Contributing](https://make.wordpress.org/cli/handbook/contributing/) 。遵循这些准则有助于与该项目的其他贡献者进行交流,他们将竭尽全力与您合作。 |
| 187 | + |
| 188 | +## 管理者 |
| 189 | + |
| 190 | +WP-CLI 项目维护者: [schlessera](http://github.com/schlessera)。 |
| 191 | + |
| 192 | +我们将写权限授予[受信任的贡献者](https://make.wordpress.org/cli/handbook/committers-credo/),这些贡献者已经证明他们有能力并有时间开发该项目。 |
| 193 | + |
| 194 | +阅读手册中的 [Governance](https://make.wordpress.org/cli/handbook/governance/),获取有关该项目的更多操作详细信息。 |
| 195 | + |
| 196 | +## 参考 |
| 197 | + |
| 198 | +除了 [composer.json](https://wp-cli.org/composer.json) 中定义的库之外,我们还使用了下面项目中的代码或思想: |
| 199 | + |
| 200 | +* [Drush](https://github.com/drush-ops/drush) 用于很多事情 |
| 201 | +* [wpshell](https://code.trac.wordpress.org/browser/wpshell) 用于 `wp shell` |
| 202 | +* [Regenerate Thumbnails](https://wordpress.org/plugins/regenerate-thumbnails/) 用于 `wp media regenerate` |
| 203 | +* [Search-Replace-DB](https://github.com/interconnectit/Search-Replace-DB) 用于 `wp search-replace` |
| 204 | +* [WordPress-CLI-Exporter](https://github.com/Automattic/WordPress-CLI-Exporter) 用于 `wp export` |
| 205 | +* [WordPress-CLI-Importer](https://github.com/Automattic/WordPress-CLI-Importer) 用于 `wp import` |
| 206 | +* [wordpress-plugin-tests](https://github.com/benbalter/wordpress-plugin-tests/) 用于 `wp scaffold plugin-tests` |
0 commit comments