Home / PHP / Fix WordPress Feed XML Parsing Errors

Fix WordPress Feed XML Parsing Errors

When you try to Export all content or rss feed in wordpress then it’ll show XML Parsing Error and not working with seo in your site and so was the WordPress source feed which showed “XML Parsing Error: XML or text declaration not at start of entity“. And all this while Google reader was correctly tracking our feed, while it was not loading in any web browser!

XML Parsing Error
Firefox showed this error on loading the feed

XML Parsing Error: XML or text declaration not at start of entity Location: http://www.quickonlinetips.com/archives/feed/ Line Number 2, Column 1: ^

Google Chrome revealed some more information

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.

I found that it was most commonly caused by a Blank line before XML declaration

Basically this error arises if there is a whitespace before an XML declaration

<?xml ... ?>

appears in your feed. The blank line error means that the code has blank lines in it, which can be easily introduced in the WordPress php code by a plugin, theme, or any configuration file.

So basically I had to go looking for blank lines before PHP starting tag

<?php
and after the PHP closing tag
 ?>

I intended to use the ‘Fix RSS Feed WordPress plugin’ which promised to search all folders (except wp-admin and wp-includes directory) if the php file has blank lines in the head and tail line, and then delete blank lines. The plugin author writes that it will delete php files at only the beginning and end of the blank lines, and will not delete the blank lines between documents, so it will not affect the operation of the php programs.

Now, change your root directory where wp-config.php include, see below

Rasel Khan:~$ cd /var/www/project/wordpress
Rasel Khan:~$ ls
index.php    wp-activate.php     wp-comments-post.php  wp-content   wp-links-opml.php  wp-mail.php      wp-trackback.php
license.txt  wp-admin            wp-config.php         wp-cron.php  wp-load.php        wp-settings.php  xmlrpc.php
readme.html  wp-blog-header.php  wp-config-sample.php  wp-includes  wp-login.php       wp-signup.php

open your ‘index.php’ file via any editor, I’m using gedit editor

Rasel Khan:~$ sudo gedit index.php

Here ‘index.php’ file look

<?php
/**
 * Front to the WordPress application. This file doesn't do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 *
 * @package WordPress
 */

/**
 * Tells WordPress to load the WordPress theme and output it.
 *
 * @var bool
 */
define('WP_USE_THEMES', true);

/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/wp-blog-header.php' );

Now create a new file called ‘fix-rss.php’ in ‘wp-config.php’ directory and copy the follwing code

<?php
function wp_xml_fix_rss($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("/As*/m", "", $input);
	} else {
		return $input;
	}
}

ob_start("wp_xml_fix_rss");

Finally include ‘fix-rss.php’ file in ‘index.php’, see below look like

<?php
/**
 * Front to the WordPress application. This file doesn't do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 *
 * @package WordPress
 */

/**
 * Tells WordPress to load the WordPress theme and output it.
 *
 * @var bool
 */
include './fix-rss.php';
define('WP_USE_THEMES', true);

/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/wp-blog-header.php' );

It’ll be works ! 🙂
if any problem just comment here………

About Rasel Khan

Check Also

How to fix qualified domain name

When you restart your apache server using the command Someone get the following error: Let’s …

One comment

  1. Great tuts ! very helpful….

Leave a Reply

Your email address will not be published. Required fields are marked *