Development

“Headers already sent” in WordPress — the cause and the fix

The full warning reads something like: Cannot modify header information — headers already sent by (output started at /path/functions.php:1). It means something printed to the browser before WordPress tried to send an HTTP header (a redirect, a cookie, a login). The good news: the message tells you exactly where.

1. Read the file and line

The important part is output started at /path/to/file.php:LINE. That is the file that sent output early, and the line where it happened — usually line 1 (before the opening tag) or the last line (after the closing tag). Open that file and go straight there.

2. Remove whitespace before <?php or after ?>

The classic cause is a blank line or a stray space before the opening <?php or after the closing ?>. Even one space counts as output. Delete it so the file begins immediately with <?php and has nothing after the final ?>.

3. Check for a hidden BOM

If the file looks clean but the error persists at line 1, it probably has a UTF-8 byte-order mark — invisible bytes some editors add. Re-save the file as UTF-8 without BOM (most code editors have this option). That silent character is “output” as far as PHP is concerned.

4. Prevent it for good

In files that are pure PHP — functions.php, plugin files, includes — remove the closing ?> entirely. The PHP manual recommends this precisely because it makes trailing whitespace impossible. With no closing tag, there is nothing after your code to leak output.

Edit safely

If the error is coming from your theme or a plugin, make the edit over SFTP with a proper code editor rather than the WordPress file editor — and always keep a backup of the file you touch, so a bad save is a one-click revert.