RSS Feeds Broken, Wordpress Plugins and Duct Tape Solutions
As some of you who use this site via its RSS feed have undoubtedly noticed over the last couple of days, those feeds pretty much entered FUBAR territory. Instead of nicely structured XML data, there was an error about the XML entity not coming at the beginning of an external file.
This weekend, Tim McGuire saw it and let me know. I cleared the cache and it seemed to go away. Of course, like all good problems, it came back and the old remedy wasn't any match for it.
So, what was the problem you ask (and lots of people on lots of forums have been asking about this very problem)?
1. If there is any space, extra line or anything at all before the initial <?xml at the beginning of the output, the strict validators will puke on it.
2. Are you serious, J? Yes. An extra empty line in front of the first text is what was breaking it.
So, it's just a matter of removing the line and my chest can swell with geeky pride, right? Well, not so fast. See, the empty line wasn't in wp-rss2.php.
Where was it? I still don't know. The general consensus on this bug was that any one of the installed plugins, on any one line (depending on the plugin) could be contributing the extra line. Which, of course resulted in a gigantic case of
Whiskey
Tango
Foxtrot
Never one to voluntarily take on the drudgery of manually digging through files while the live system is up and running, I whipped up a quick solution that fixes it in the short term, while I try to figure out which one of the 25+ plugins is outputting a single empty line.
So, I used output buffering and PHP's trim() function to just strip off extra whitespace before and after the content. The stupid extra line is still there, but is suppressed, making the feed OK for use. In other words, the tumor is still there, but the headache is hidden by aspirin.
If you'd like to put this change in for your own Wordpress site while you, too, play "Where's Whitespace" with your code tree, do this:
Add:
ob_start();
as the first line inside the PHP snippet in wordpress/wp-rss2.php (and wp-rss.php, etc. for the different feed types).
Then, at the bottom of the file, add this snippet.
<?php
$output = ob_get_contents();
ob_clean();
print(trim($output));
ob_flush();
?>
If you're using any plugins that mess with output buffering already or any fancy header control, this may not work, but it did on this site and patched things over while I play oncologist and go digging for the little tumor.
While I'd be irritated at the author of whatever plugin is causing the problem, to me every tool along the way should be working against this being a problem in the first place. I understand how putting an ampersand inside the file would cause an error. But, come on, whitespace inside the tags is ignored, so why is the same whitespace inserted in the beginning enough to make the parsers choke? And, why doesn't Wordpress already do something similar to my fix to prevent poorly written plugins from breaking the whole thing because they hit an extra ENTER key.
Beyond them, why does PHP implicitly flush the headers at the first sign of whitespace? Or at least allow you to turn it off? Wordpress isn't the only PHP app to run into this problem. Forums are full of people completely clueless as to why they're getting errors only to discover that that little extra line at the top of the file is breaking their entire script.

April 18th, 2006 at 6:44 pm
Arrrgh!!
Having the same problem with one of our users and the AMM plugin and I can't figure it out because activating AMM on my own blog doesn't cause the problem.
He swears blind AMM is the cause, so… where should I be looking for whitespace?
See I was a little confused, not sure if I should be looking for rogue echo's or space bwteen or after tags. I always thought they caused "headers sent out" but maybe I'm wrong.
Any extra information you can give me would be greatfully received.
Kindest regards,
Denyerec
May 1st, 2006 at 7:02 am
Thanks for the extremely useful posting - it seems a huge number of people are going bananas trying to work this one out. Whitespace!!!! Go figure!!!! A little crazy in this day and age, but there ya go. Will try out your little suggestion… though I must admit that I've disabled all plugins and that still has no effect. Bah! See my posting here:
http://kmi.open.ac.uk/people/marc/2006/05/01/broken-rss-20-feed-in-wordpress-caused-by-mystery-file-mystery-whitespace/
September 5th, 2006 at 10:17 pm
The whitespace is probably coming from php include files that end in ?>, because there's usually a newline or two after that.
When I write PHP code, I always omit the trailing ?> from my include files. It's perfectly legal PHP and it avoids the whitespace problems.
December 11th, 2006 at 3:35 am
Dwayne got it spot on for me!
After a day trying to work out why wp-rss2.php and xmlrpc.php didn't work, I deleted the trailing ?> from wp-config.php and wp-settings.php. That solved the problems completely. No need for the hack.
I guess the older versions of PHP (in my case v4.4.0) don't manage the trailing space so well.
For the record, on PHP 5.2, my local machine, no problems. I'm using Wordpress 2.0.5.
February 28th, 2007 at 10:38 pm
THANK YOU so much for solving an issue that seems to give me headaches every time I start modifying my site! I very much appreciate the help!
April 3rd, 2007 at 1:00 pm
Thanks for the work-around! Fortunately, I only had to deactivate 2 plugins to find the one causing trouble — Role Manager. That one broke the visual editor on another blog after trying to install a different editor, so I think I'm just going to set Role Manager aside!
April 30th, 2007 at 3:42 pm
I just started having this issue, I didn't notice the broken feed until now, but there were so many things I changed in the past few weeks that I don't know what actually caused it. I have tried the hack above, doesn't work. I am thinking maybe it has something to do with the Optimal Title plugin, but I am not sure. I will have to copy over the header file in my theme with a fresh one and see what happens.
September 8th, 2008 at 9:40 am
Thanks, that helped me with the plug-in I am working on.
I can't believe white space is causing so much havoc. This is quite outrageous really.
October 3rd, 2008 at 6:21 am
Hi,
I found out a more general solution:
http://wejn.org/stuff/wejnswpwhitespacefix.php.html
after spending several hours yesterday trying to fix this very issue. Maybe you'll find the script helpful.