Home » Wordpress » How to: Fix XML Parsing Error of WordPress Feed

How to: Fix XML Parsing Error of WordPress Feed

AIM– “To fix error on line 2 at column 6: XML declaration allowed only at the start of the document”

Scenario – WordPress Feeds

Situation – Error



Symptom –  Due to whitespace before the beginning line of xml feed.

Problem – Either due to extra space after closing tag ?> in functions.php of your WordPress theme file.

Simple Fix (1 hour fix)- Try deactivating your theme and check your blog.com/rss  for no more errors. If no error then theme is causing you the problem. So either find the whitespace in your php file and remove it. If it it not in functions.php, the error might be from any of the theme files.

Very very simple fix (1 minute fix)Add the following code to your functions.php of your theme.

function ___juniorhero_wp_whitespace_fix($input) {
	/* valid content-type? */
	$allowed = false;

	/* found content-type header? */
	$found = false;

	/* we mangle the output if (and only if) output type is text/* */
	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;

	/* do the actual work */
	if ($allowed || !$found) {
		return preg_replace("/\\A\\s*/m", "", $input);
	} else {
		return $input;


Once you add the above code, boom the xml parsing error is gone. Enjoy your blogging days.

, ,