XML declaration allowed only at the start of the document
Asked Answered
H

7

8

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

Havard answered 4/2, 2013 at 11:31 Comment(0)
S
28

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

Sepulcher answered 13/9, 2019 at 15:55 Comment(2)
Outstanding -- we've been having this issue with our sitemap for weeks now, this absolutely fixed it. Many thanks.Mannerheim
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.Carcinogen
K
14

Your xml document starts with a new-line.

2024 update

I hope this solved your issue back in 2013. For those finding this question and answer today, the typical reason for an extra newline to appear at the start of the output of a PHP script (whether that was an XML file, RSS feed or HTML page), the common cause for this often 1 specific reason:

One of your PHP files, whether it's one you wrote or any PHP file you included via composer ends with a ?> with an extra newline. (?>\n\n)

Most modern PHP style guides will recommend against using the closing ?> for this very reason. Anything outside of <?php and ?> becomes output to the browser/HTTP client, and while it's easy to spot a <?php starting on line 2, a ?> with some extra lines at the bottom of the file is easy to miss.

Any lines outside of <?php and ?> gets echoed straight to the client and runs before any functions or classes you've defined unless you started 'output buffering' before including any file.

This is also the number one cause for the common headers already sent error.

Your files should not end with ?>. If you do this, remove them. If you did, then the cause of this issue is one of your dependencies/plugins.

Kampmann answered 4/2, 2013 at 11:34 Comment(2)
Where can I find my sitemap file, not found in wp root.Beem
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.Beem
S
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, ...).

Sudor answered 12/3, 2018 at 19:51 Comment(0)
P
1

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/

Pathe answered 1/10, 2014 at 1:4 Comment(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.Bine
G
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.

Garget answered 18/7, 2019 at 13:23 Comment(0)
A
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.
}
Adamik answered 6/7, 2022 at 14:47 Comment(0)
R
0

<?php enter code here // Your PHP code here ?> enter code here // Closing tag omitted to avoid trailing whitespace

Rimrock answered 27/7 at 2:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.