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

WordPress 6.2网站Requests PSR-0类废弃警告的彻底修复求助

WordPress 6.2中Requests库PSR-0类弃用警告的彻底解决方法

问题背景

网站基于WordPress 6.2,近期出现以下弃用警告:

<br />
<b>Deprecated</b>:  The PSR-0 `Requests_...` class names in the Request library are deprecated. Switch to the PSR-4 `WpOrg\Requests\...` class names at your earliest convenience. in <b>/home/username/public_html/wp-includes/Requests/src/Autoload.php</b> on line <b>171</b><br />

该警告会随AJAX请求返回,导致页面功能异常。目前已临时注释掉Autoload.php中load函数内的trigger_error调用(代码如下),但这只是临时方案:

public static function load($class_name) {
    // Check that the class starts with "Requests" (PSR-0) or "WpOrg\Requests" (PSR-4).
    $psr_4_prefix_pos = strpos($class_name, 'WpOrg\\Requests\\');

    if (stripos($class_name, 'Requests') !== 0 && $psr_4_prefix_pos !== 0) {
        return false;
    }

    $class_lower = strtolower($class_name);

    if ($class_lower === 'requests') {
        // Reference to the original PSR-0 Requests class.
        $file = dirname(__DIR__) . '/library/Requests.php';
    } elseif ($psr_4_prefix_pos === 0) {
        // PSR-4 classname.
        $file = __DIR__ . '/' . strtr(substr($class_name, 15), '\\', '/') . '.php';
    }

    if (isset($file) && file_exists($file)) {
        include $file;
        return true;
    }

    /*
     * Okay, so the class starts with "Requests", but we couldn't find the file.
     * If this is one of the deprecated/renamed PSR-0 classes being requested,
     * let's alias it to the new name and throw a deprecation notice.
     */
    if (isset(self::$deprecated_classes[$class_lower])) {
        /*
         * Integrators who cannot yet upgrade to the PSR-4 class names can silence deprecations
         * by defining a `REQUESTS_SILENCE_PSR0_DEPRECATIONS` constant and setting it to `true`.
         * The constant needs to be defined before the first deprecated class is requested
         * via this autoloader.
         */
        if (!defined('REQUESTS_SILENCE_PSR0_DEPRECATIONS') || REQUESTS_SILENCE_PSR0_DEPRECATIONS !== true) {
            // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
            // trigger_error(
            //     'The PSR-0 `Requests_...` class names in the Request library are deprecated.'
            //     . ' Switch to the PSR-4 `WpOrg\Requests\...` class names at your earliest convenience.',
            //     E_USER_DEPRECATED
            // );
            // Prevent the deprecation notice from being thrown twice.
            if (!defined('REQUESTS_SILENCE_PSR0_DEPRECATIONS')) {
                define('REQUESTS_SILENCE_PSR0_DEPRECATIONS', true);
            }
        }

        // Create an alias and let the autoloader recursively kick in to load the PSR-4 class.
        return class_alias(self::$deprecated_classes[$class_lower], $class_name, true);
    }

    return false;
}

经排查,触发警告的类为Requests_IDNAEncoder,但未在自定义代码中调用该类,推测由第三方程序(插件/主题)触发。

彻底修复方法

1. 定位调用源头

通过调试追踪调用栈,找到触发Requests_IDNAEncoder的具体文件:

  • 在wp-config.php中开启调试日志:
    define('WP_DEBUG', true);
    define('WP_DEBUG_LOG', true);
    
  • 在Autoload.php的trigger_error代码块(或注释位置)前添加以下代码,打印调用栈:
    if ($class_name === 'Requests_IDNAEncoder') {
        error_log(print_r(debug_backtrace(), true));
    }
    
  • 触发AJAX请求后,查看wp-content/debug.log,从中找到调用Requests_IDNAEncoder的插件或主题文件路径。

2. 替换弃用类为PSR-4规范类

  • 如果调用源头是自定义代码:将Requests_IDNAEncoder替换为WpOrg\Requests\IDNAEncoder
  • 如果是第三方插件/主题:
    • 优先检查插件/主题是否有更新版本,更新后通常会修复这类弃用问题
    • 若无更新,可手动修改对应文件中的类名(注意备份文件,后续插件/主题更新会覆盖修改内容)

3. 规范静默警告(替代修改核心文件的临时方案)

不推荐直接修改WordPress核心文件(更新会覆盖修改),官方提供了合法的静默方式:
在wp-config.php中添加以下常量:

define('REQUESTS_SILENCE_PSR0_DEPRECATIONS', true);

此方法不会修改核心文件,且能有效阻止警告输出,适合暂时无法找到调用源头的场景。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 09:43:17