6

My blog feed show error today:

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

My blog feed: http://feeds.feedburner.com/klassicblog

My blog: http://blog.klassicweb.com

halfer
  • 19,824
  • 17
  • 99
  • 186
samir panchal
  • 140
  • 1
  • 1
  • 3

6 Answers6

25

Here is the solution I found First you create a php file (whitespacefix.php) on the wordpress root dictory with following content.

<?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");
?>

Then open the index.php file and add the following line right after <?php tag

include('whitespacefix.php');

Referenced from here

Asce4s
  • 819
  • 3
  • 10
  • 16
  • Outstanding -- we've been having this issue with our sitemap for weeks now, this absolutely fixed it. Many thanks. – jimiayler Dec 17 '19 at 19:38
  • If you're on a cloud host where you cannot edit the index.php file you can include it in the wp-config for the same result. – Dustin Snider Jan 10 '22 at 04:14
12

Your xml document starts with a new-line.

Evert
  • 93,428
  • 18
  • 118
  • 189
  • Where can I find my sitemap file, not found in wp root. – Silver Ringvee Oct 11 '16 at 08:10
  • 1
    As the question is related to Wordpress, so is the answer. Both are related to sitemap as well, therefore the location of sitemap is a good place to start with when applying what you have suggested. – Silver Ringvee Oct 11 '16 at 19:25
3

Every page on your page (included sitemaps) contains empty line at the begin. (You can see HTML source) You should remove this line to fix sitemaps. You can try following steps:

Check your wp-config.php file for blank lines outside of bracketed sections.

Check your theme’s functions.php file for blank lines outside of bracketed sections.

One by one, disable plugins and revalidate until you isolate the one causing the problem ( How To Check For Plugin Conflicts )

Note that the final php ?> should be omitted from all PHP code files - modules, includes, etc. (eg. wp-config.php, functions.php, ...).

Prince Ahmed
  • 1,038
  • 10
  • 10
0

There must be more than 1 blank line at the very end of the functions.php file if you are using wordpress.

Just remove these blank lines and the error would go off.

If you want to know more about the error, read this article: http://www.am22tech.com/fix-xml-declaration-rss-feed-error/

Anil Gupta
  • 11
  • 1
  • The article you link to says that in order to avoid the error you can have NO MORE THAN ONE blank line at the end of functions.php. – skybondsor Jun 23 '15 at 18:55
0

One of the files included in either your theme, or a plugin, has an empty blank line before the starting

First, deactivate all plugins, and try to load your sitemap.xml link. If it works now, then one of your plugins is the culprit. If it still does not work, then move on...

Second, go through each and every one of the .php files that make up your WordPress theme. Find any starting

Third, reinstall WordPress core installation. If it still doesn't work, then contact a developer to do some deep diving into your source code.

R Diaz
  • 22
  • 4
0

The problem is a poorly formatted plugin file or functions file, most likely containing an additional empty line at the tail of a file.

I resolved this by opening the WordPress plugins list in one tab:

/wp-admin/plugins.php

In a second tab open the sitemap:

/sitemap_index.xml

Then simply follow this logic until you find the cause:

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.
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Jason Is My Name
  • 929
  • 2
  • 14
  • 38