如何以经过身份验证的用户身份运行WP-CLI命令?

时间:2019-12-09 作者:Lucky The Rabbit

我写了一个命令wp-cli 调用wp nurse. 它是根据wp doctor 命令,但用于运行WordPress站点运行状况检查。使用浏览器中的工具,当前所有16个测试都通过。但是,当从wp, 只有13项测试成功。

ichabod:~ uplime% wp nurse check --all
+----------------------+-------------+--------------------------------------------------------+
| name                 | status      | message                                                |
+----------------------+-------------+--------------------------------------------------------+
| wordpress_version    | good        | Your version of WordPress (5.3) is up to date          |
| plugin_version       | good        | Your plugins are all up to date                        |
| theme_version        | good        | Your themes are all up to date                         |
| php_version          | good        | Your version of PHP (7.3.12) is up to date             |
| sql_server           | good        | SQL server is up to date                               |
| php_extensions       | good        | Required and recommended modules are installed         |
| utf8mb4_support      | good        | UTF8MB4 is supported                                   |
| https_status         | good        | Your website is using an active HTTPS connection.      |
| ssl_support          | good        | Your site can communicate securely with other services |
| scheduled_events     | good        | Scheduled events are running                           |
| http_requests        | good        | HTTP requests seem to be working as expected           |
| debug_enabled        | good        | Your site is not set to output debug information       |
| rest_availability    | recommended | The REST API encountered an unexpected result          |
| dotorg_communication | good        | Can communicate with WordPress.org                     |
| background_updates   | critical    | Background updates are not working as expected         |
| loopback_requests    | recommended | Your site could not complete a loopback request        |
+----------------------+-------------+--------------------------------------------------------+
Error: Only 13/16 tests passed
深入研究这些测试的代码,看起来它们由于没有经过身份验证而失败了(rest_availability, background_updates, 和loopback_requests). 例如background_updates 看起来要呼叫GET /wp-admin/site-health.php?health-check-test-wp_version_check=1, 这失败了,因为从技术上讲,我没有登录仪表板。除了创建临时管理员并使用该管理员登录(这听起来像是一个糟糕的解决方法),还有什么方法可以临时使用WordPress进行身份验证,以便这些测试能够通过?

1 个回复
最合适的回答,由SO网友:leymannx 整理而成

默认情况下,WP-CLI以未经身份验证(注销)的用户身份执行每个命令。要以任何现有WordPress用户的身份执行命令,可以使用全局--user 参数,该参数接受用户ID、登录名或电子邮件地址。

$ wp nurse check --all --user=1
运行时将列出所有其他全局参数$ wp --help$ wp COMMAND --help.

GLOBAL PARAMETERS

  --user=<id|login|email>
      Set the WordPress user.

相关推荐

即使路径中有WP-CLI,也会出现“无法打开输入文件”错误

我正在使用XAMPP本地开发Wordpress,目前正在尝试安装WP-CLI工具as described here 使用Cygwin。我重命名了wp-cli.phar 到wp, 使其可执行并将其移动到XAMPP/php 文件夹但是,运行wp 告诉我错误:无法打开输入文件:/cygdrive/b/Users/User/Desktop/XAMPP/php/wp另一方面./wp 从文件所在的目录运行程序,不会出现问题。尽管事实上/cygdrive/b/Users/User/Desktop/XAMPP/php 和