WordPress: Cannot modify header information – headers already sent by…

wordpress-header-modified-error

“Cannot modify header information – headers already sent by..”? This is most common issue faced by user while customising wordpress header cookie. If you are working on wordpress theme or writing a new plugin for wordpress, you might have seen a warning which unable you to modify header content. We faced this problem when we are working on single sign on plugin.

We start searching on google and found lots of forums, blogs, threads etc. explaining the reason of issue. Most of them suggesting that it is related with white space in  wp-config.php file, opening and closing tag (<?php ?>) of php. We have tried many solutions e.g. remove the white space, check the opening and closing tags etc. but did not get success.

Found something which helps

We read in one of the article saying it is related with output_buffering settings at php.ini. output_buffering setting unable user to modify header cookie multiple times. By default PHP disable the output_buffering in ini file.

output_buffering = 4096

Now we knew the problem, but we do not have the access to php.ini file. So we google “how to allow enable output_buffering on”. There is many ways to enable the output_buffering. We add ob_start() line at start of our function code. It works and we are able to modify header cookie without any trouble.

ob_start()

Plugin works smoothly, and we are happy until one of our client faced the problem again. We checked the code, ob_start() code is there but it did not worked. It was really annoying and head scratching for us.

dinesh5507f6ae468b6

We tried other solutions to enable output_buffering e.g. ini_set(), ob_flush() , but none of them worked for us. Then we decide to add custom php.ini file to our wordpress root directory and write one line code in it. Adding php.ini in root directory of wordpress will override the settings of server php.ini file. So it override the setting and enable the output_buffering.

output_buffering=on

Guess what, It works. All have started working and plugin works smoothly. We google for the reason why it was not working through ini_set and ob_start. In safe mode environment ini_set and ob_start does not work.

Conclusion

This blog is going help you to get rid of the “cannot modify header..” error.  Feel free to contact us if you have any further queries

 

WordPress: Cannot modify header information – headers already sent by… was last modified: March 18th, 2015 by Dinesh Kumar