Broken RSS Feed Due to Blank Lines in PHP Code


Today, I discovered that my blog had a broken RSS feed. The feed wasn’t showing up correctly. Actually, it wasn’t showing up at all in Mozilla Firefox — instead it tried to download the feed as a file. And it was displayed as XML source code in other browsers.

Broken RSS Feed

Here is what my visitors saw in their browsers when their tried to access’s RSS feed… RSS feed in Mozilla Firefox RSS feed in Mozilla Firefox RSS feed in Google Chrome RSS feed in Google Chrome RSS feed in Internet Explorer RSS feed in Internet Explorer

Broken RSS feed in Opera RSS feed in Opera

Check RSS Feed Validator

I immediately started investigating this problem.

The first thing I did was check the validity of my RSS feed using the W3C Feed Validation Service. feed validation results feed validation results

And the reason for the error was very clear right away. Somehow the feed’s source code contained blank lines before the actual XML declaration.

This caused that all browsers displayed my blog’s feed as gibberish.

Finding the Culprit

By googling for a little while, I found out that this is a very widespread problem for WordPress users.

So widespread indeed that one person even created a whitespace fix for it.

While the fix is a nice effort, I needed to resolve the cause of the problem and not only cover up the symptoms.

By reading a few WordPress user comments on various forums, I started to understand what was happening.

The WordPress RSS feed processor has a hard time integrating (or rather ignoring) blank lines in the site’s PHP code. Most often extra blank lines in the functions.php file were causing the trouble.

Checking Out functions.php

So, next, I went to my WordPress Dashboard > Appearance > Editor and selected the functions.php file from the list of files on the right side of the Editor’s page.

When I scrolled down to the bottom of the functions.php file, the problem was there clear as day…

Blank lines in the functions.php file

Blank lines in the functions.php file

The blank lines were actually added by me when I edited the functions.php file some days ago.

The reason I added the blank lines was to make the PHP code easier to read and understand.

However, to achieve the goal of better code readability and modularity, I erroneously added the PHP end tag ?> after a piece of code. Then started up the next piece of code with a PHP begin tag <?php.

While this solution may seem harmless, it actually created the white spaces or blank lines outside of the PHP code. And that resulted in my broken RSS feed.

The Solution

To resolve the matter I just removed the PHP end tags and begin tags where they were superfluous.

So the code now looks liked this:

Blank lines removed from functions.php

Blank lines removed from functions.php

With the unnecessary PHP end tags ?> and the PHP begin tags <?php gone, the RSS feed started to work again.

Check it out for yourself:

Liked this post?

Subscribe to our newsletter to receive early notification of new posts and deals:
[mc4wp_form id=”45″]

Next Post »« Previous Post