Fixing the “Cannot Modify Header” Error in WordPress

Here’s a frustrating yet common WordPress error that can appear out of the blue, bringing down your site in one fell swoop:

“Warning: Cannot modify header information – headers already sent by (output started at…”

What’s worse is that this error continues, listing paths to important files on your server which is a potential security risk.

But not to worry! It’s one of the easier errors to resolve and in this post I’ll show you how to fix it as well as how to hide error messages from showing on the front-end of your site.

Where Does the Error Come From
How to Fix the “Cannot Modify Header”Error
Fix #1: Upload a Fresh Copy
Fix #2:  Edit the Trouble File

Still Can’t Fix the Error?

Where Does the Error Come From

Sure, this error may seem intimidating and almost like there’s an issue with your theme’s header.php file or something similar, but it actually has to do with extra spacing in one of your files. Sometimes they get added on unintentionally or automatically and need to be adjusted.

The error displays itself when there are:

Blank lines at the top or bottom of a document
Too many spaces before, in between or after the beginning <?php and ending ?> PHP tags
There are too many unneeded spaces or blank lines in a specific part of a document

In order to fix the spacing issue, you need to first know where it’s happening. Fortunately, the error message lets you know which file and line is affected. Keep in mind that you may see this sequence multiple times on the page which means there are multiple files that need fixing.

Here’s the basic structure to look out for in order to decipher the broken file:

Warning: Cannot modify header information – headers already sent by (output started at /path/to/file/broken-file.php:#) in /path/to/file/affected-file.php on line #

The first file listed as /path/to/file/broken-file.php would be the one that needs fixing and the # character directly after it would be replaced with a number which would tell you which line you need to fix.

The second file written in this structure as /path/to/file/affected-file.php is the file that isn’t able to work properly because the first file is broken. The # symbol at the end in a real world example would be a number which indicates the line in the document that’s affected and not able to work.

Here’s an example of what this error could look like on your site:

Warning: Cannot modify header information – headers already sent by (output started at /home/name/you-site/wp-content/themes/your-theme/custom-functions.php:1) in /home/name/your-site/wp-includes/pluggable.php on line 876

In this example, name would be the username of your cPanel or hosting account and your-site would be replaced with the folder name where your site is stored. The error you would actually see would look differently, but it should give you a good idea of what to expect.

In this example, custom-functions.php within my theme’s folder is the document that needs fixing and the error is on line one.

Fortunately, it’s not terribly difficult to fix this issue even though it may seem intimidating at first.

Read on for your options.

How to Fix the “Cannot Modify Header”Error

There are two main ways you can fix the “Cannot modify header” error. If you get stuck along the way, feel perfectly free to get in touch with our expert support team. They can also help you with just about anything WordPress-related, they’re available 24/7 and they’re happy to help you out.

They would otherwise be eagerly waiting until you ask them a question so you may as well stop by if you run into troubles, especially when premium and expert support is included in your WPMU DEV membership. If you’re not a member, you can still try us out for free and ask an expert for help.

Fix #1: Upload a Fresh Copy

When you read the error message and you determine that the broken file is a part of the WordPress core or it’s otherwise an inherent file of a plugin or theme, there’s a quick fix available.

If it’s a regular plugin or theme file, you can uninstall and re-install it.

If you would rather not do this or a WordPress core file is affected, you can download a fresh copy and extract the ZIP archive to your computer. Locate the file that’s broken on your site and replace the existing one on your server with the fresh copy.

You can do this with the help of FTP, SSH or directly through cPanel.

Fix #2:  Edit the Trouble File

Once you have identified the problem file, you can download a copy of it through FTP or SSH, or you can edit it directly in cPanel.

Clear any extra blank lines or spaces at the beginning and end of the document with your backspace button or Delete button for PC. Also, be sure there are no spaces before, in between or after the <?php tag at the beginning of the file. The same goes for the ?> tag at the end.

If the final line of the document doesn’t end in a ?> tag, then be sure there are still no spaces after the final character of the last line.

Save the file and upload it back to your site in the same place, overwriting the original file.

Still Can’t Fix the Error?

If you try these fixes and you still see the same error for different files or lines, that’s normal. Repeat the steps and keep clearing those extra spaces or blank lines.

If you see the exact same error as before, here are some extra considerations for you to help you double check your work:

If you clear all the extra spaces and lines but press any other key in the process, it could bring the issue back so try again if this happens.
Make sure there aren’t multiple code blocks in your PHP file. You should only have one opening <?php tag at the beginning and one ?> closing tag at the end.
Be sure the encoding of the file is set to UFT-8. Make this change if it’s set to something else and try the steps again.
Use a proper code editor if you download a copy of the file and save it to your computer.
Don’t use common word editing programs such as MS Word since they add extra formatting to documents that with brake your site further.

Disabling Front-End Error Reporting

By now, you should have your files fixed and the pesky “Cannot modify header” error should be as good as gone. Still, nothing’s stopping those errors from coming back and being visible to everyone who visits your site, including hackers.

It’s not the worst thing to happen to your site, but revealing real, live file paths on your server could give a hacker the exact information they need to target your site. Instead of guessing where you files are to try to hack them, they would see them displayed in plain sight. Nothing would stop them from hacking that file directly.

You can turn off front-end error reporting and enable a private log with WP_DEBUG. You can check out all the details in one of our other posts Debugging WordPress: How to Use WP_DEBUG.

Editor’s Note: This post has been updated for accuracy and relevancy. [Originally Published: Sept 2016 / Revised: February 2022]

Щелкните ниже, чтобы поставить оценку!
Всего: 0 В среднем: 0

Написать комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *

10 + 20 =