title: "编写用于验证 WordPress 文件内容的检查项" post_status: publish comment_status: open taxonomy: category: - wp-cli-handbook post_tag: - Doctor - Guides - Repos


编写用于验证 WordPress 文件内容的检查项

wp doctor 中包含的检查类型之一是 File_Contents,即能够检查所有或部分 WordPress 文件是否符合给定的正则表达式模式。该检查类型已被多个默认诊断检查使用,你也可以在自定义的 doctor.yml 配置文件中使用 File_Contents 检查类型。

File_Contents 检查类型是检查 WordPress 文件最有效的方式,因为它只遍历文件系统一次,无论你注册了多少个文件检查项。

例如,以下是两个使用 File_Contents 的检查项,一个确保插件未使用会话,另一个确保 wp-config.php 中未使用 $_SERVER['SERVER_NAME']

file-sessions:
  check: File_Contents
  options:
    regex: .*(session_start|\$_SESSION).*
    only_wp_content: true
file-server-name-wp-config:
  check: File_Contents
  options:
    regex: define\(.+WP_(HOME|SITEURL).+\$_SERVER.+SERVER_NAME
    path: wp-config.php

一起运行时,你可能会看到:

$ wp doctor check --config=file-contents.yml --all
+----------------------------+---------+-----------------------------------------------------------------------------------------+
| name                       | status  | message                                                                                 |
+----------------------------+---------+-----------------------------------------------------------------------------------------+
| file-sessions              | success | 所有 'php' 文件通过 '.*(session_start|\$_SESSION).*' 检查。                             |
| file-server-name-wp-config | success | 所有 'php' 文件通过 'define\(.+WP_(HOME|SITEURL).+\$_SERVER.+SERVER_NAME' 检查。        |
+----------------------------+---------+-----------------------------------------------------------------------------------------+

File_Contents 检查类型接受以下选项: