How to Fix “Headers Already Sent” Errors in WordPress

Sometimes WordPress outputs a strange error message called “Cannot modify header information – headers already sent.” Your blog crashes and displays nothing other than the error message. How do we fix this?

While the exact cause can vary, the underlying problem is the same. Let’s say you’re trying to redirect the request to some other page, like here, for example, where you’re trying to hide your author information by switching to the home page. The above error will appear if you try and output something before the redirect occurs. Essentially, you’ve already started the page loading process in the browser either by sending the headers or some output and can’t go back.

Here are some possible causes for this:

Debugging Info like printf Statements etc

Here’s an error I got recently while trying out some code when redirecting a page in WordPress:

Headers already sent error message
“Headers already sent” error message

The error message includes the line causing the output in brackets. In my case, it was line 52. Opening my code and going to line 52, I noticed that I’d printed out an error message before causing the redirect like this:

No Output before Redirect
No Output before Redirect

You can see the “printf” statement before the “wp-redirect” function. Since printf already sends some output to the browser, it’s too late to create a redirect. Removing debugging statements before changing headers or printing stuff to the screen (or even the browser console) will remove the “headers already sent” message.

Remove Extraneous lines between ?> and <?php Tags

Sometimes the PHP file can send output very subtly. Suppose you have a section in your code where you close the PHP tag and then open it again after a couple of lines like this:



Of course, you won’t typically code like this from scratch. But it can quickly occur if you’re copying and pasting code from sources that enclose their code in PHP tags. If you try and change WordPress headers or redirect after this “gap,” you’ll get the “headers already sent” message.

Save PHP files as “UTF-8 without BOM”

Often you will code your PHP files in an editor like Notepad++. While saving your files, you must ensure that you select the “Encode in UTF-8 without BOM” option under “Encoding” like this:

Encode without BOM
Encode without BOM

“BOM” stands for “Byte order mark” and is a sequence of characters at the start of the file to indicate that it’s encoded in UTF-8. The Linux environment of PHP doesn’t recognize this character and treats it as text, which means that it’s sent as an output to the browser. Oops!

You’ll most often encounter the “BOM” problem when attempting to set cookies. You’ll get an error message like this: “Cannot send session cookie – headers already sent.” If this is the case, check the encoding you use for your PHP files. Remove the “BOM” in your UTF-8 files, and you’re golden.

Hopefully, this article will help you troubleshoot your error and fix your code. Good luck!

About Bhagwad Park

I've been writing about web hosting and WordPress tutorials since 2008. I also create tutorials on Linux server administration, and have a ton of experience with web hosting products. Contact me via e-mail!

Speak Your Mind