Broken RSS Feed Due to Blank Lines in PHP Code

happy-kitten

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 stosu.com’s RSS feed…

stosu.com RSS feed in Mozilla Firefox

stosu.com RSS feed in Mozilla Firefox

stosu.com RSS feed in Google Chrome

stosu.com RSS feed in Google Chrome

stosu.com RSS feed in Internet Explorer

stosu.com RSS feed in Internet Explorer

Broken RSS feed in Opera

stosu.com 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.

stosu.com feed validation results

stosu.com 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: stosu.com/feed.

Liked this post?

Subscribe to our newsletter to receive early notification of new posts and deals:

Next Post »« Previous Post

Leave a Reply

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