Posted in WordPress by
Anthony Hortin
(Updated: March 10, 2012)

I had my day planned out nicely. Only a few minor updates to make on my WordPress theme for my new site and I would’ve been done (coding wise). Then it happened! I logged into the admin Dashboard to make a few content tweaks and things seemed a bit strange. When I tried to save some content updates, the page just wouldn’t refresh properly. I even tried logging out of the dashboard… But still no luck šŸ˜„

Next up, I tested the site to see if it was up and running ok. On the surface things looked fine but then I noticed that when I tried to type in a URL without the trailing forward-slash (‘/’), the page wouldn’t appear. Normally, depending on how you’ve configured your Permalinks, it will simply add the trailing slash automatically. Instead, all I got was a blank white screen starring back at me. Needless to say, a few expletives were uttered!

I tried thinking back to when everything seemed to be working fine. I also tried to remember what changes I’d made the day before. From memory, it was mainly some CSS updates to change links colours. There were no major code updates from what I could remember. I also couldn’t work out why the URL’s weren’t being processed correctly. After doing some Googling, I decided to raise a (low priority) support ticket with my hosting provider to see if any server changes had been made within the last few days. It was as if the .htaccess rules weren’t being obeyed. Good and bad news. The good news… no changes had been made to the server. The bad news… this didn’t get me any closer to resolving my issue.

The next step was to try changing to the default WordPress theme and do some testing. Things seemed to improved after changing themes. I could save my pages consistently. I could log in and out of the Dashboard without a problem. Even the URL’s worked fine, with or without the trailing slashes. Hmmm It would appear that the culprit was my own theme, which I’d been lovingly hand crafting. Next test was a restore from a backup of my theme. Luckily I’m quite strict at performing backups. I perform a backup of my workstation and my development server every couple of days, with the files kept on a separate hard drive. I rotate between 4 different sets for each machine. I restored the oldest backup which was just over a week old. Sure enough, everything worked fine. The next backup I tried was one from only two days prior. This also worked fine. Things were starting to look up! What this meant was that I’d narrowed it down to being caused by the changes that I’d made the previous day.

I now needed to compare the latest version of my code (the one with the problems) to the version from two days prior. I downloaded and installed WinMerge, which is an extremely handy Open Source differencing and merging tool (for Windows). Luckily there weren’t too many files changed. As mentioned earlier, most of the changes were in my main stylesheet. Starting from a copy of my restored (working) theme, I slowly started to integrate the changes that I’d made the previous day. Most seemed fairly mundane. A few CSS changes and some very minor code updates. I then got to my functions.php file. I incorporated the changes and lo and behold, things broke immediately. Yay!! I’d found the damn file that was causing all my problems! Now it was simply a matter of narrowing down the exact cause.

Looking through the code reminded me of the changes that I’d made. I’d added a few comments to the code to document a couple of the functions (Silly me, I know! Real developers don’t document code!!). The other thing I’d done was to add a blank line in between where I’d closed a PHP tag and opened up the next one. The code snippet is question looked like this…

You’ll notice that between the closing PHP tag and the following opening tag, there’s a blank line. Gasp!! This is what was causing all the drama! The solution was to simply remove the offending blank line. In fact, since I was simply closing the PHP tag and immediately reopening it, I could easily omit both of them. The resulting code looked like…

As for the problems that it caused, well there were quite are few.

URL’s weren’t processed properly

Since my Permalinks were setup using the format “/%year%/%monthnum%/%postname%/“, the posts wouldn’t display properly if they didn’t have the trailing slash. Normally, this is something that is handled by your .htaccess file. Instead, I was simply receiving a blank page unless I specifically had the slash on the end of the URL.

Strange content appearing

This was a strange one. As you can see in the image below, characters such as quotes and apostrophes were being rendered incorrectly. You’d think this was because their was some ‘strange’ characters stored in the database, but nope, it displayed perfectly fine once functions.php was fixed.

Blank lines appearing at top of document source, before the DOCTYPE

One of the repercussions of the blank line “incident” was that it caused blank lines to appear before the DOCTYPE within the html. This can cause problems with your RSS feed, in particular with Feedburner. Blank lines (outside of PHP tags) in any of your page templates can also cause the same problem, so I made sure I thoroughly checked those as well.

Problems in the WP Dashboard

As mentioned above, one of the first things that alerted me to the problem was my administration dashboard becoming unstable. I found that whenever I tried to save some content changes, the page wouldn’t refresh. Also, I couldn’t even log out of the WordPress Dashboard. I was able to browse through the various menu options within the Dashboard, but when it came to performing an action such as saving a page, that’s when the problems surfaced.

Due the the ‘strangeness’ of some of the problems, this was one bug that took a while to track down. Thankfully though, it’s all fixed now. It’s certainly something that I’ll be more mindful of in the future when I create themes.

Have you come across this problem or any other strange issues? I’d love to hear, so feel free to leave a comment šŸ™‚

16 responses on “Never Ever Have Blank Lines Outside Your WordPress PHP Tags

  1. sickdesigner

    Very helpful, Anthony! This post came just in time as Iā€™m literally knee deep in a functions . php file right now, so great save!
    It seems that the problems the blank line caused go as deep as they can seeing as even the dashboard was affected.

    1. Anthony Hortin

      Thanks. Glad you found it useful. It was a real strange one this, having it cause problems not only with the website but with the admin dashboard as well. It took a while to figure it out and that was one of the reasons why I wanted to document it somewhere, so it’ll hopefully help others. Thanks for the comment šŸ™‚

  2. Andy Gongea

    Thank you for posting this article. Same thing happened to me and I was next to going crazy. Thanks once again.

    Cheers and take care.

    1. Anthony Hortin

      My pleasure Andy. Uuugh! I feel your pain! For the life of me, I couldn’t work out what was going on. Glad I had some backups that I could go back to and compare against. Thanks for your comment šŸ™‚

  3. David W.

    I once had an include file that had an extra, unseen blank line at the very end of the file after the ?>. I kept getting “headers already sent” errors and couldn’t figure out why! Took me six hours of frantic stabs in the dark before I realized what happened. A really humbling experience šŸ™‚

    I’ve heard recommendations to omit the closing ?> at the end of a document for this reason, and claims that this was legal PHP syntax. Anyone have experience with that?

    1. Anthony Hortin

      What a pain! I think the most frustrating thing is when it’s something small like that and it takes hours and hours to debug. Gets really annoying when you “waste” all that time finding something so insignificant. It is valid syntax to leave off the closing PHP tag but personally, I always close mine. I think I’d be too worried that it was going to cause some other unforeseen problem further down the track. Thanks for the comment David šŸ™‚

  4. thierry

    Thanks thanks thanks !!!!!!! Merci merci merci (i’m french…..)
    3 days losts with this fu….. blank lines….. and it was the same problem : a blank line in my functions.php page !
    Thanks again !

  5. Ro Dolman

    Thanks so much!! I am just getting into theme development – working on a starter theme for myself. My functions.php had some spaces and I searched everywhere for a solution – thought I was going to have to start from scratch again! Another step solved in my learning curve šŸ™‚

    1. Anthony Hortin

      I’m glad my post was able to help you out. That’s great to hear. Sounds like you’ve got it all sorted now, which is even better. Thanks for the comment šŸ™‚

  6. Quizzer

    hi, thanks for the info i have the same problem but i no have blank lines oustise the php code :S have another solution using a function ? or editing some core wordpress file? thanks

    1. Anthony Hortin

      That’s a tough one. You’d need to go through all your theme files to check out any code or updates that you’ve done. I presume it works ok if you change themes? Also, try disabling all your plugins to check that it’s not one of them that’s causing your problems.

  7. Wan

    Great post! I have encountered this kind of the problem few months ago. It’s very sensitive kind of thing when we try to tweak some php coding in WordPress. I am also trying to figuring out how do i insert a blank line between a flash file and a time/date post. I have tried using or kind of thing but to date unsuccessful. Anyone care to help?

    1. Anthony Hortin

      Thanks for the comment šŸ™‚ In regards to your other question… I’m not sure I fully understand the problem, but can you simply enclose your Flash file within a div and just add some extra top/btm margin to it?

  8. Enrico

    Ah ah, so fun! šŸ™‚ I’ve just lost my saturday afternoon to solve this same problem and, after having found the solution by myself (and without any file compare utility), I stepped on your page!
    Well, +1 for my debugging experience… but I’d really prefer to have my saturday afternoon back! šŸ˜‰