← Tous les articles

“Cannot modify header information – headers already sent”: the fix

This PHP error breaks redirects and logins. It nearly always comes from output sent too early, and the message tells you exactly where.

“Cannot modify header information - headers already sent”: the fix

Key points

  • PHP must send HTTP headers before any content.
  • The message names the file and line that sent output too early.
  • Usual culprit: whitespace after ?> or a UTF-8 BOM.

You cannot log in, redirects stop working, and you see: “Warning: Cannot modify header information – headers already sent by (output started at …)”. The part in brackets is the key.

Read the message

“output started at /wp-content/themes/mytheme/functions.php:58” means that line printed something (a space, a newline, an echo) before PHP tried to send a header. Look there, not in the file that raises the warning.

The three classic causes

  • Whitespace after the closing ?> tag at the end of a PHP file. Easy fix: drop the final ?>; it is pointless in a pure PHP file.
  • A UTF-8 BOM, invisible at the start of the file, added by some editors. Save as “UTF-8 without BOM”.
  • A leftover echo or var_dump in a hook that runs before the page is sent.

The typical WordPress case

A function on init prints something, then a wp_redirect() runs later. Simple rule: on init, template_redirect or admin_post_*, process and redirect, never print. And always exit; after wp_redirect().

add_action('template_redirect', function () {
    if (is_page('old-page')) {
        wp_safe_redirect(home_url('/new-page/'), 301);
        exit;
    }
});

FAQ

Can I just hide the warning?

You can, with output_buffering, but you hide the cause and redirects or logins stay fragile. Fix the line it points to.

It started after I edited functions.php

Check the end of the file: whitespace after ?> is the most likely cause.

Facing this on your site? We diagnose and fix it without putting your content at risk.

Partager LinkedIn Facebook X WhatsApp
</>

webtestudio

Agence web : sites WordPress sur mesure, thèmes premium et logiciels SaaS.

Parler de votre projet →