Site icon Hip-Hop Website Design and Development

How to Fix the “Cannot Modify Header Information – Headers Already Sent By” Error

Most WordPress error messages give you an idea of what’s causing problems on your site. The “Warning: cannot modify header information – headers already sent by” error is no exception. If a PHP file cannot be executed due to a problem in its code, you’ll run into this message.

There are several potential causes for the “Cannot modify header information” error. Fortunately, the message itself will tell you which file is causing the problem. It even points to the line of code that contains the issue.

In this article, we’re going to discuss this error and its causes. Then, we’ll go over two ways that you can fix the problem. Let’s get to work!

What Causes the “Cannot Modify Header Information – Headers Already Sent By” Error

As we mentioned before, you’ll run into this error when one of your site’s .php files cannot be executed. WordPress relies on .php files, such as wp-config.php and functions.php, for its core functionality.

If there’s a problem within one of the .php files that your website needs to load, you’ll see an error message that looks like this:

Warning: Cannot modify header information - headers already sent by (output started at /home/public_html/wp-config.php:#) in /home/public_html/wp-includes/file-example.php on line 33

Fortunately, the “Cannot modify header information” error provides a lot of information that makes troubleshooting relatively straightforward. The message will point you toward two files – the first one contains the problem, which prevents the second one from executing.

At the end of the error message, you’ll see a section that says “line XX.” It shows the location of the specific code that’s causing the problem.

Usually, the problem in the PHP code is pretty easy to fix. Some common causes that can trigger the error message include:

Fixing these types of errors requires you to be at least passingly comfortable with modifying PHP code. You won’t need to add any code yourself.

Still, you may need a bit of extra help identifying the problem. This is particularly true if the issue isn’t related to whitespaces or statements before the PHP header function.

body a.novashare-ctt{display:block;background:#00abf0;margin:30px auto;padding:20px 20px 20px 15px;color:#fff;text-decoration:none!important;box-shadow:none!important;-webkit-box-shadow:none!important;-moz-box-shadow:none!important;border:none;border-left:5px solid #00abf0}body a.novashare-ctt:hover{color:#fff;border-left:5px solid #008cc4}body a.novashare-ctt:visited{color:#fff}body a.novashare-ctt *{pointer-events:none}body a.novashare-ctt .novashare-ctt-tweet{display:block;font-size:18px;line-height:27px;margin-bottom:10px}body a.novashare-ctt .novashare-ctt-cta-container{display:block;overflow:hidden}body a.novashare-ctt .novashare-ctt-cta{float:right}body a.novashare-ctt.novashare-ctt-cta-left .novashare-ctt-cta{float:left}body a.novashare-ctt .novashare-ctt-cta-text{font-size:16px;line-height:16px;vertical-align:middle}body a.novashare-ctt .novashare-ctt-cta-icon{margin-left:10px;display:inline-block;vertical-align:middle}body a.novashare-ctt .novashare-ctt-cta-icon svg{vertical-align:middle;height:18px}body a.novashare-ctt.novashare-ctt-simple{background:0 0;padding:10px 0 10px 20px;color:inherit}body a.novashare-ctt.novashare-ctt-simple-alt{background:#f9f9f9;padding:20px;color:#404040}body a.novashare-ctt.novashare-ctt-simple-alt:hover,body a.novashare-ctt.novashare-ctt-simple:hover{border-left:5px solid #008cc4}body a.novashare-ctt.novashare-ctt-simple .novashare-ctt-cta,body a.novashare-ctt.novashare-ctt-simple-alt .novashare-ctt-cta{color:#00abf0}body a.novashare-ctt.novashare-ctt-simple-alt:hover .novashare-ctt-cta,body a.novashare-ctt.novashare-ctt-simple:hover .novashare-ctt-cta{color:#008cc4}The silver lining to seeing this error message- you already know which file is causing the problem and the line of code with the issue! Learn how to fix it here Click to Tweet

How To Troubleshoot the “Warning: Cannot Modify Header Information – Headers Already Sent By” Error (2 Methods)

There are two approaches to troubleshooting the “Cannot modify header information – headers already sent by” error. The first method doesn’t require you to exit the WordPress dashboard.

However, the second strategy uses FTP/SFTP if you can’t access the dashboard or use WordPress.

Let’s start with the first troubleshooting method.

1. Fix the Error With the Plugin/Theme Editor or Replace a Plugin

The first thing you need to do when you run into the “Cannot modify header information – headers already sent by” error is to open the file that’s causing the problem. Then, locate the line the message indicates.

For example, if you see an error that reads the following, it means you need to look inside your theme’s functions.php file:

Warning: Cannot modify header information - headers already sent by (output started at /home/public_html/wp-content/themes/twentytwentyone/functions.php:#) in /home/public_html/wp-includes/file-example.php on line 1

In this scenario, you can reach the source of the problem using the WordPress theme editor. To access it, go to Appearance > Theme Editor.

Once you’re in, use the menu to the right to select the file you need to access.

Sign Up For the Newsletter

Theme functions file (functions.php).

If you look closely, you’ll notice several whitespaces before the <?php tag. The error message itself points toward line number one. Therefore, this tells you that the whitespaces are the sources of the problem.

In this example, all you have to do is remove the whitespaces and click on Update File. Now try reloading your website, and the error should be gone.

You can apply the same process using the WordPress plugin editor (Plugins > Plugin Editor). This method is applicable if the error message points toward a faulty plugin file.

Alternatively, you may run into an error that indicates one of the files within your WordPress plugins directory. In this scenario, you can remove and reinstall that plugin. In most cases, that will take care of the issue for you.

However, keep in mind that you might lose that plugin’s configuration, depending on which tool you use. As such, you may need to set up the add-on again.

2. Edit the Problem File via FTP/SFTP

In some cases, the source of the “Cannot modify header information – headers already sent by” error won’t lie in a file that you can access using the WordPress theme or plugin editors. Alternatively, you might be using a non-WordPress site.

In these scenarios, your best option is to access the problem file using FTP/SFTP. To do so, you’ll need to use an FTP or SFTP client such as the FileZilla platform.

You’ll also need access to your website’s FTP/SFTP credentials. In most cases, you should be able to find them within your hosting panel.

Need blazing-fast, reliable, and fully secure hosting for your ecommerce website? Kinsta provides all of this and 24/7 world-class support from WooCommerce experts. Check out our plans

If you use Kinsta, you can access MyKinsta, select your website under Sites and click on its Info tab.

SFTP/SSH in MyKinsta.

Once you have the credentials, use your FTP or SFTP client to connect to your website. You’ll need to locate the site’s root folder. Usually, its name should be root, public_html, public, or your own site’s name.

Here’s a quick look at what the inside of a WordPress root folder looks like.

WordPress root folder.

Go ahead and locate the file that the “Cannot modify header information – headers already sent by” error indicates. For example, if the issue is public/wp-config.php, right-click on the file and select the View/Edit option.

Click on the wp.config file.

That option will open the selected file using your default text editor. Once the document is open, locate the problem by navigating to the line the error message pointed you toward.

Look for the line with the error message.

If you can’t spot the error, you might need to consult with someone who has experience working with PHP files. However, suppose you’re dealing with a whitespace issue or a statement before the PHP header. In that case, you should be able to fix the problem yourself.

Once you’re done, save the changes to the file and close the FTP/SFTP client. Try reaccessing your website, and the error should be gone.

Seeing this error message? This post has 2 guaranteed ways tod fix it Click to Tweet

Summary

The “Warning: cannot modify header information – headers already sent by” error can be intimidating because it outputs a long message. However, that detailed error message makes this bug relatively simple to troubleshoot. Unlike other problems, this one is polite enough to tell you which file is causing it and which line of code you need to look into.

Depending on the file that’s causing the error, there are two ways that you can go about troubleshooting it:

  1. Fix the error using the plugin/theme editor or replace a plugin.
  2. Edit the problem file via an FTP/SFTP client.

Finding the source of this error is simple. However, fixing it can be a problem if you’re not familiar with PHP.

Still having issues fixing this error? Please share your experience with our community in the comments below!

The post How to Fix the “Cannot Modify Header Information – Headers Already Sent By” Error appeared first on Kinsta®.