Solve Cannot modify header information error in WordPress

Some functions modifying the HTTP header are:

header / header_remove
session_start / session_regenerate_id
setcookie / setrawcookie

Output can be:


Whitespace before <?php or after ?>
UTF-8 Byte Order Mark
Previous error messages or notices
print, echo and other functions producing output (like var_dump)
Raw <html> areas before <?php code.
Why does it happen?

To understand why headers must be sent before output it's necessary to look at a typical HTTP* response. PHP scripts mainly generate HTML content, but also pass a set of HTTP/CGI headers to the webserver:

HTTP/1.1 200 OK
Powered-By: PHP/5.3.7
Vary: Accept-Encoding
Content-Type: text/html; charset=utf-8

<html><head><title>PHP page output page</title></head>
<body><h1>Content</h1> <p>Some more output follows...</p>
and <a href="/"> <img src=about:note> ...

The page/output always follows the headers. PHP is required to pass the headers to the webserver first. It can only do that once. And after the double linebreak it can't ever append to them again.

When PHP receives the first output (print, echo, <html>) it will "flush" the collected headers. Afterwards it can send all the output bits it wants. But sending further headers is impossible from then.

If you have functions.php file into your current theme directory, then do as below into your functions.php file

// allow redirection, even if your theme starts to send output to the browser
add_action('init', 'clean_output_buffer');
function clean_output_buffer() {
0.00 avg. rating (0% score) - 0 votes