空白字符导致sitemap无法显示

文章 ID: 614
分类: 随记
标签:
最后更新:

This page contains the following errors:
error on line 2 at column 6: XML declaration allowed only at the start of the document
Below is a rendering of the page up to the first error.问题仍然没有解决

Warning: Your WordPress installation has a problem with outputting extra whitespace. This can corrupt backups that you download from here. UpdraftPlus备份里也提示这个错误。2个错误有关联

这两个错误是相关的!它们都指向同一个问题:WordPress安装中存在额外的空白字符输出。主要影响:

  1. Sitemap错误:
    • XML声明必须在文件最开始
    • 额外空白字符导致XML解析失败
  2. UpdraftPlus备份错误:
    • 备份文件可能被额外空白字符污染
    • 可能导致备份文件损坏

统一解决方案:

  1. 首先在functions.php最开头添加(第一行):

    亲测:解决了问题

<?php
if (!function_exists('remove_whitespace_everywhere')) {
    function remove_whitespace_everywhere() {
        ob_start(function($buffer) {
            // 移除多余空白,但保留XML文档
            if (strpos($buffer, '<?xml') !== false) {
                return $buffer;
            }
            return trim($buffer);
        });
    }
    add_action('init', 'remove_whitespace_everywhere', 0);
}
  1. 检查以下所有PHP文件的开头和结尾:
    • wp-config.php
    • functions.php
    • submit-to-baidu.php
    • wordpress-optimizations.php
    • index.php

确保:

  • 文件以<?php开始(前面无空行)
  • 删除所有文件末尾的?>
  • 使用UTF-8无BOM编码保存
  1. 临时禁用插件测试:
// 在wp-config.php中添加(用于测试)
define('DISABLE_WP_CRON', true);

发表评论

您的邮箱地址不会被公开。 必填项已用 * 标注

滚动至顶部