仅允许在文档开头使用 XML 声明

问题描述 投票:0回答:7

我的博客提要今天显示错误:

此页面包含以下错误:

第 2 行第 6 列错误:仅允许在文档开头使用 XML 声明

下面是出现第一个错误之前的页面渲染

我的博客提要:http://feeds.feedburner.com/klassicblog

我的博客:http://blog.klassicweb.com

xml wordpress feed feedburner
7个回答
28
投票

这是我找到的解决方案 首先,在 WordPress 根目录上创建一个包含以下内容的 php 文件 (whitespacefix.php)。

<?php
function ___wejns_wp_whitespace_fix($input) {
    $allowed = false;
    $found = false;
    foreach (headers_list() as $header) {
        if (preg_match("/^content-type:\\s+(text\\/|application\\/((xhtml|atom|rss)\\+xml|xml))/i", $header)) {
            $allowed = true;
        }
        if (preg_match("/^content-type:\\s+/i", $header)) {
            $found = true;
        }
    }
    if ($allowed || !$found) {
        return preg_replace("/\\A\\s*/m", "", $input);
    } else {
        return $input;
    }
}
ob_start("___wejns_wp_whitespace_fix");
?>

然后打开index.php文件并在

<?php
标签后面添加以下行

include('whitespacefix.php');

参考自这里


13
投票

您的 xml 文档以换行符开头。


3
投票

页面上的每个页面(包括站点地图)开头都包含空行。 (您可以查看 HTML 源代码)您应该删除此行来修复站点地图。您可以尝试以下步骤:

检查您的 wp-config.php 文件中括号部分之外的空白行。

检查主题的functions.php 文件中括号部分之外的空行。

逐一禁用插件并重新验证,直到找出导致问题的插件(如何检查插件冲突)

请注意,所有 PHP 代码文件中应省略最后的 php ?> - 模块、包含文件等(例如 wp-config.php、functions.php 等)。


1
投票

如果您使用的是 WordPress,则functions.php 文件的末尾必须有超过 1 个空行。

只要删除这些空行,错误就会消失。

如果您想了解有关该错误的更多信息,请阅读这篇文章: http://www.am22tech.com/fix-xml-declaration-rss-feed-error/


0
投票

您的主题或插件中包含的文件之一在开始之前有一个空行

首先,停用所有插件,然后尝试加载您的 sitemap.xml 链接。 如果现在有效,那么罪魁祸首就是您的某个插件。 如果仍然不起作用,那么继续...

其次,浏览构成 WordPress 主题的每一个 .php 文件。查找任意起始点

第三,重新安装WordPress核心安装。 如果仍然不起作用,请联系开发人员深入研究您的源代码。


0
投票

问题是插件文件或函数文件格式不正确,很可能在文件尾部包含额外的空行。

我通过在一个选项卡中打开 WordPress 插件列表解决了这个问题:

/wp-admin/plugins.php

在第二个选项卡中打开站点地图:

/sitemap_index.xml

然后简单地按照这个逻辑,直到找到原因:

Deactivate five plugins starting from the top.

if (sitemap shows without errors) {
    Activate four of the latest five to be deactivated and visit the sitemap again.

    if (sitemap shows without errors) { 
        The currently deactivated plugin is at fault. You must reach out to the plugin developer or format the code removing the extra spacing at the beginning or tail of the file.
    else {
        Activate the deactivated plugin and choose one of the other four and deactivate.
    }
} else {
    Reactivate these five deactivated plugins and move onto the next five plugins and deactivate.
}

0
投票

<?php enter code here
// 这里是你的 PHP 代码
?> enter code here
// 省略结束标记以避免尾随空格

© www.soinside.com 2019 - 2024. All rights reserved.