Ari Stathopoulos

Web Developer, Accessibility & Sustainability evangelist, Human.

The Headers already sent by issue

27 September 2014

On the support forums I work it’s not uncommon to see an PHP error like this: Cannot modify header information - headers already sent by....

In a WordPress installation with a dozen plugins and 3rd party themes this is actually not that hard to occur!

Since this is something that I see at least a couple of times each week, I figured I’d write an article here to avoid re-typing the same stuff each time. :)

This message indicates that somewhere on your installation a php file outputs headers before its time. Usually you can solve this issue by following the process below:

  1. Switch your theme to the default twentyfourteen theme and deactivate all plugins. You should now see that this error no longer exists. To find out which plugin/theme triggers this issue, start activating them one-by-one and test again. Finally, activate your previously selected theme.
    As soon as the error reappers, you probably found the theme or plugin that causes this (though not 100%… the last plugin could be just the trigger and not the actual cause).

Next, you’ll have to inspect the files of the plugin that triggers this error.

Open all php files one by one and check for the following:

If there is a space or blank line before the opening <?php tag on the file, delete those spaces or lines.

If there is a closing ?> tag at the end of the file, delete it.

As stated in the PHP docs:

The closing tag of a PHP block at the end of a file is optional, and in some cases omitting it is helpful when using include or require, so unwanted whitespace will not occur at the end of files, and you will still be able to add headers to the response later. It is also handy if you use output buffering, and would not like to see added unwanted whitespace at the end of the parts generated by the included files.

The usual suspects for this erros are the functions.php and wp-config.php files so you might want to check these 2 first.

There are more things that can cause this (for example Output Control Function) but the above are the most common causes.

I hope that helps someone!

Cheers, Ari.