Skip to content
01

WordPress “headers already sent” error: find the faulty line and fix it

“Cannot modify header information, headers already sent” gives you the exact file and line of the problem. Here is how to read them and fix the four classic causes.

Walid Sadfi
4 min reading
Share:

The full message looks like this: Warning: Cannot modify header information, headers already sent by (output started at /path/to/file.php:12). It often comes with confusing symptoms: unable to log into the dashboard, redirects that do not happen, cookies that are not saved, a blank page after saving. Good news: it is one of the easiest PHP errors to fix, because it names the culprit itself.

What is actually happening

An HTTP response starts with headers: redirects, cookies, content type. PHP must send them before a single character of the page. If something has already been printed, even a single space, the headers are gone and WordPress can no longer redirect or set a cookie. Hence the failed logins and redirects.

Reading “output started at”

The message contains two locations. The one that matters is in brackets, after output started at: it is the file and line that printed something too early. The other location only shows where WordPress tried to send a header, and it is rarely at fault. Open the quoted file at the line given.

The four classic causes

Whitespace or a blank line around the PHP tags

The number one cause: a space before the opening php tag, or a blank line after the closing tag, in functions.php or wp-config.php. The modern rule is simple: in a file containing only PHP, do not put a closing tag at all. Remove it, and any whitespace after it disappears with it.

An invisible BOM at the start of the file

Some editors save files as UTF-8 with BOM, three invisible bytes placed at the very start. To PHP, they are printed characters. If the error points to line 1 of a file that does start with the php tag, save it again as UTF-8 without BOM from your editor.

PHP errors displayed on screen

A simple PHP warning printed in the page counts as output and in turn triggers headers already sent. In production, errors must go to a log, never to the screen:

define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );

Code that prints at the wrong time

A plugin or theme that echoes in a hook that runs too early, such as init or plugins_loaded, prints content before the headers. Any redirect must happen before any output, in the template_redirect hook, followed by exit:

add_action( 'template_redirect', function () {
    if ( ! is_user_logged_in() && is_page( 'client-area' ) ) {
        wp_safe_redirect( home_url( '/login/' ) );
        exit;
    }
} );

What about output buffering?

Turning on output_buffering in the PHP configuration or calling ob_start() hides the symptom: headers then go out correctly. That is acceptable as an emergency workaround, not as a fix. The stray output still exists and can break a JSON response or an RSS feed.

Frequently asked questions

I can no longer log into the dashboard, what should I do?

Fix the file quoted by the message over SFTP. If it is the theme's functions.php, temporarily rename the theme folder: WordPress falls back to a default theme and logging in works again.

The error appeared after pasting code found online, why?

Those snippets often contain their own closing php tag followed by a blank line. Remove the closing tag at the end of the file. If you would rather have a developer handle it, our WordPress maintenance service fixes this kind of error within the day.

Need a developer to fix the problem or keep it from coming back? Discover our WordPress maintenance service.

Author

Walid Sadfi

Walid Sadfi is the founder of Evolurise, a team of engineers deployed on site with its clients: custom development (WordPress, Drupal, React, Laravel), AI agents, DevOps and team training. With 8 years of experience, he supports SMEs and large companies, from understanding the problem to deploying the solution. On this blog he shares practical, field-tested insights.