Your website will require constant updating and maintenance, always checking it is running in the most perfect condition and no error is present, because this means you will lose visitors, will experience an increase in the bounce rate, and a decrease in conversions. The most common errors will interrupt the proper functioning of your website, that is why you must always be in the loop on what are the most common causes and how to fix them.
We’ve shared our comprehensive guides on the most common WordPress errors and how to troubleshoot them, for this article we will be showing you how to fix cannot modify header information error, as it is a highly prevalent error that appears whenever a .php request cannot be executed because the output is being sent before calling an HTTP header.
What’s good about this error is that it displayed the .php file that is experiencing the issue and the exact line where the error is occurring. This means it is not all that hard to find the proper solution and fix cannot modify header information error.
What Are the Causes of the “Cannot Modify Header Information – Headers Already Sent By” Error?
While this error might seem intimidating, it is not in fact at all, because you can easily troubleshoot and fix cannot modify header information. If this error is displayed on your website it simply means is there an extra white space that is not supposed to be on your functions.php file.
These white spaces can be added unintentionally or automatically and need to be adjusted. As we’ve previously discussed, the “Cannot Modify Header Information – Headers Already Sent By” error will occur only when there is an input that’s being sent before calling the HTTP header.
In WordPress, this error will be caused when there’s a faulty code on your page header and it will look something like this:
Warning: Cannot modify header information – headers already sent by (output started at /public_html/wp-content/plugins/my-plugin/my-function.php:#) in /public_html/wp-includes/pluggable.php on line #
As you can see, two files are being mentioned the first file is the one that is broken and prevents the other from properly functioning, while the second file is the one that is affected. This first line tells us where code is broken and this is where you should be focusing on fixing.
The code line # indicates the exact location of the line that’s causing the error. This helps you locate the error quickly without having to scour the entire code to look for the culprit. The most causes of the “Cannot Modify Header Information – Headers Already Sent By” error are caused by excessive white space in the code or by sending the output before calling the header.
The error will indicate exactly where the culprit is and you will be able to fix cannot modify header information error in no time. The error is caused either by:
- White space before the opening tag (if one is present)
- An HTML output is being sent before the calling header
- Issues with an installed plugins
- An HTML block is in front of the header within the .php file.
How to Fix Cannot Modify Header Information - Headers Already Sent By Error?
There are a few ways to fix cannot modify header information. The warning message will tell you where you need to check to fix the issue, as we mentioned look for the folder file that is broken and the code line that needs to be fixed that is stated after the line # piece of text. You will need to access and check the code within the wp-config.php file.
Edit the Corrupted File
If you have extra white space in the corrupted file you can easily access it either by using an FTP Client or cPanel. Either way all you need is access to the wp-config.php file. It is advisable to backup your WordPress website before you start tweaking the files in case something goes south.
If the faulty file has white spaces this is the easiest way to fix cannot modify header information error. Once you are logged in on your FTP Client or cPanel, open the public_html folder and find the wp-config.php file.
Open the file by clicking upon it and select Edit. This is when you find the code line specified in the error message. Delete any white space or blank lines in the file using the Delete button or the Backspace button.
Also, make sure you remove any white space preceding the closing tag make sure you remove the extra space there too.
Click on Save Changes and close the file.
Now it’s time you put your hard work to the test. Go back to your website, refresh the page that the error “Cannot Modify Header Information – Headers Already Sent By” has occurred. If the error disappears then you have successfully managed to troubleshoot it, if not, please follow the other steps we will show you.
Relocate the Header Statement
By default, your website’s header should be called first before the body. Any raw HTML element that’s located in the .php file is rendered as a direct output. If an HTML element is placed before the header call, meaning if any output has been called before your site’s header, then it causes the error to occur. To fix cannot modify header information error you must place the HTML block after the header statement. Here’s what an incorrect code will look like
< html > <php header('Location: http://www.yourwebsite.com/'); ?>
All the functions that produce outputs must be placed after the header statement and follow the HTTP header call. You can review and scan the .php file that’s specified in the warning message and fix the code.
Replace the File
When you first see the “Cannot Modify Header Information – Headers Already Sent By” error and the warning message tells you that the broken file is located in the WordPress core file there’s a simple solution to that. You can replace the WordPress file with a fresh installation. To do that
- Download the latest WordPress installation on your computer
- Use an FTP Client or cPanel to open and replace the file to your server
- Locate the file that’s corrupted, find the one that’s specified in the warning message
- Upload the new file you just downloaded, and overwrite the existing corrupt file with the new version
That’s all it takes. Now what you must do is test if replacing the corrupted file has worked. Go to your website and refresh the page. As per usual if the error has gone, it was all completed successfully.
Deactivate the Plugin that is at Fault
A faulty error can also cause the error to be displayed. To fix cannot modify header information error when a plugin is at fault, it is quite a simple process, one that we always do. Simply deactivate each plugin one by one. To do that
- Go to the WordPress dashboard
- Click on Plugins > Installed Plugins
- Deactivate each plugin one by one
- Upon deactivation of each plugin, go back to your website and refresh the page to see if the error has been resolved
- Continue this process until your find that one of the plugins has caused the error “Cannot Modify Header Information” to appear.
Pay close attention to recently installed plugins and see if they are at fault for causing the error. Check to see what plugin has crashed your site’s code and inform their developer’s team of the incident.
Disable the Front-end Reporting
Yes, we agree that the “Cannot Modify Header Information – Headers Already Sent By” error is quite frustrating, another reason as to why you should fix cannot modify header information error as soon as possible is due to security measures . The error can appear out of the blue. The warning message that you used to decipher the code as to where the culprit is, is the same code that will also have your website at risk if people with bad intentions find it.
By now you must have gotten rid of the “Cannot Modify Header Information” error, but it might still occur in the future. What you can do to prevent your website from malicious attacks is to disable the front-end reporting screen. Because if these URL paths are live on your website for everyone to see, you can provide the hacker the exact information they need to target your site. To turn off the front-end reporting you should enable WP_DEBUG mode. You can debug your website either using an FTP Client or cPanel.
- You will first need to locate the public_html folder
- Next, locate the wp-config.php file and click to open it
- Now you should paste a piece of code onto the file. Scroll down to where WP_DEBUG is defined, it should look something like this define (‘WP_DEBUG’, false); or if you cannot find it paste the code right before the line that says “/*That’s all, stop editing! Happy blogging.*/:
- This is the piece of code you will need to paste into
define (‘WP_DEBUG’, true);
define (‘WP_DEBUG_LOG’, true);
define (‘WP_DEBUG_DISPLAY’, false);
@ini_set (‘display_errors’, 0);
This is how you enable WP_DEBUG mode on WordPress using either cPanel or an FTP client. Remember to keep your website updated and safe.
As of the end of this article, we’ve shared our step-by-step tutorial on how to fix cannot modify header information error and provided a comprehensive guide on the most common causes as well as how to troubleshoot it. Oftentimes the “Cannot Modify Header Information” error will be displayed as a result of unnecessary blank space on your site’s code, or when output is being called before your site’s header.
These are the four ways to fix cannot modify header information error
- Edit the corrupted file
- Relocate the header statement
- Replace the core file
- Deactivate faulty plugins
You can try to edit the corrupted file and relocate the header statement approaches first as they are the most likely cause for the error to appear. Because this error will tell you exactly where to look for the faulty code, you save time and trouble trying to scour your site’s entire code to find the issue. The warning message on the error will show you the exact file path and line code number as to where you can try and fix the issue immediately. Tell us if you’ve tried any of the methods mentioned above and if they have worked for you in the comment section below.