WordPress RSS Feed Error

You ever get an error like this from your feeds generated by WordPress?

XML Parsing Error: XML or text declaration not at start of entity
Location: http://www.yoursite.tld/utils/test.xml
Line Number 2, Column 1:<?xml version=”1.0″ encoding=”UTF-8″?>

Somewhere along the line a new line was being placed before the XML declaration in the feeds. This caused an error to display in Firefox and other browsers instead of the feed.

I searched high and low to find where it was coming from. Disabled all me plugins, removed blank lines from code etc. To no avail. Then I noticed that the error was happening on a Mac but not in Windows. So it had to do with something about how Windows, Mac, and Linux/Unix deal with carriage returns and new line characters. I didn’t think I’d ever find the ghost new line (or return) so I tried to find a way to delete it.

Here is the hack/fix. What I’ve done is start an output buffer, and then clear it before the XML/RSS template is loaded.

In the index.php file put ob_start() like this:

<?php
ob_start();

In wp-includes/functions.php add ob_end_clean() to the beginning functions that load the feed templates.


/**
* Load the RDF RSS 0.91 Feed template.
*
* @since 2.1.0
*/
function do_feed_rdf() {
ob_end_clean();
load_template( ABSPATH . WPINC . '/feed-rdf.php' );
}

/**
* Load the RSS 1.0 Feed Template
*
* @since 2.1.0
*/
function do_feed_rss() {
ob_end_clean();
load_template( ABSPATH . WPINC . ‘/feed-rss.php’ );
}

/**
* Load either the RSS2 comment feed or the RSS2 posts feed.
*
* @since 2.1.0
*
* @param bool $for_comments True for the comment feed, false for normal feed.
*/
function do_feed_rss2( $for_comments ) {
ob_end_clean();
if ( $for_comments )
load_template( ABSPATH . WPINC . ‘/feed-rss2-comments.php’ );
else
load_template( ABSPATH . WPINC . ‘/feed-rss2.php’ );
}

/**
* Load either Atom comment feed or Atom posts feed.
*
* @since 2.1.0
*
* @param bool $for_comments True for the comment feed, false for normal feed.
*/
function do_feed_atom( $for_comments ) {
ob_end_clean();
if ($for_comments)
load_template( ABSPATH . WPINC . ‘/feed-atom-comments.php’);
else
load_template( ABSPATH . WPINC . ‘/feed-atom.php’ );
}

This worked without the change to the index.php file, but I don’t think it should have.

I tried to accomplish this with a plugin, but it wouldn’t work. since this is editing WordPress core files, these changes will need to be made again after an upgrade if the problem persists with the new version.

4 Responses

  1. Aeroboys Says:

    same problem in my blog gadgetzones.com/feed, my blog engine wordpress-2.8.4…

    I do not know what to do for my blog :(

  2. laqrhead Says:

    Try disabling all your plugins.

  3. Aeroboys Says:

    I Try disabling all plugins, I am downgrade wordpress-2.8.4 to wordpress-2.7. uninstall wordpress and install new wp engine ( export import content ), but all failed… :(

  4. Kyle Says:

    still having problems with this one

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.