You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VSCode中PHPCS检测WordPress废弃函数仅识别至4.9.0版本问题

解决WordPress废弃功能检测不生效问题

项目当前配置

composer.json 配置

"require-dev": {
    "php": "^7.4 || ^8.0",
    "dealerdirect/phpcodesniffer-composer-installer": "1.0.0",
    "phpcompatibility/php-compatibility": "^9.3.5",
    "phpcompatibility/phpcompatibility-wp": "2.1.4",
    "squizlabs/php_codesniffer": "3.7.2",
    "wp-coding-standards/wpcs": "2.3.0"
  },
  "config": {
    "allow-plugins": {
      "dealerdirect/phpcodesniffer-composer-installer": true
    },
    "platform": {
      "php": "7.4"
    }
  }

phpcs.xml.dist 配置

<ini name="error_reporting" value="E_ALL &amp;#38; ~E_DEPRECATED" />

<!-- Configs -->
<config name="minimum_supported_wp_version" value="6.0" />
<config name="testVersion" value="7.4-" />

<!-- PHP Compatibility -->
<rule ref="PHPCompatibilityWP"/>

<!-- Rules -->
<rule ref="WordPress-Docs" />
<rule ref="WordPress-Extra" />

问题

检测WordPress废弃功能时,仅能识别4.9.0及更早版本的废弃项。例如wp_make_content_images_responsive()自5.5.0起已废弃,但未触发任何报错。

修复步骤

1. 升级phpcompatibility-wp版本

当前使用的2.1.4版本仅支持检测到WordPress 5.4左右的废弃项,需升级到适配WordPress 6.0+的版本:

composer require --dev phpcompatibility/phpcompatibility-wp:^2.2.0

2. 调整phpcs配置,启用WordPress废弃功能检测规则

PHPCompatibilityWP仅负责PHP兼容性检测,WordPress自身功能的废弃检测需依赖WPCS的核心规则。修改phpcs.xml.dist:

<ini name="error_reporting" value="E_ALL &amp;#38; ~E_DEPRECATED" />

<!-- Configs -->
<config name="minimum_supported_wp_version" value="6.0" />
<config name="testVersion" value="7.4-" />

<!-- PHP Compatibility -->
<rule ref="PHPCompatibilityWP"/>

<!-- 启用WordPress废弃功能检测规则 -->
<rule ref="WordPress-Core">
    <rule ref="WordPress.WP.DeprecatedFunctions"/>
    <rule ref="WordPress.WP.DeprecatedParameters"/>
    <rule ref="WordPress.WP.DeprecatedConstants"/>
</rule>

<!-- 保留原有规则 -->
<rule ref="WordPress-Docs" />
<rule ref="WordPress-Extra" />

3. 清理缓存并重新检测

清除PHPCS缓存确保新配置生效:

phpcs --clear-cache

再重新执行检测命令:

phpcs path/to/your/code-directory

补充说明

wp_make_content_images_responsive()的废弃检测属于WordPress.WP.DeprecatedFunctions规则范畴,必须启用该规则才能触发对应报错;升级phpcompatibility-wp是为了适配更高版本WordPress的PHP兼容性相关废弃项。

内容的提问来源于stack exchange,提问作者user1956396

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 13:52:51