6
Php

Headers Already Sent Error in WordPress

In WordPress admin/back end it says :

Warning: cannot modify header information – headers already sent by (output started at /home/…../header.php:3) in /home/…. on line xxx

Well, this is a very common problem and old too, which has been discussed/resolved in so many places on the WEB (IMO) and it happens, when developing a plugin in WordPress or other operations that is being happened in WordPress back end.

Also, it’s worth mentioning that, this is not only related to WordPress but it could be happened in any Php script and related directly with Apache server. The error occurs when someone tries to modify http header, in other words, when someone uses redirect (using plain header() function in php or wp_redirect() in WordPress) somewhere in the script.

Why it happens, because server sends the response header first, before it sends the response body and this response header is sent only, when the script prints something on the screen and it could be using echo or any HTML or even if you have any empty space/comment before <?php at the top of the script, for example, something like this



In the above script, the header will be sent to the browser, when the interpreter reaches the first line (html comment) and after that if someone tries to redirect using php’s native header() function or WordPress’ wp_redirect() function, the error will be thrown, because, the redirect happens from the User Agent (browser) and the header() function, for example, header("Location: http://example.com", TRUE, 302) will send a redirect response header (with a 3xx status code) to the browser with the target url, and browser will receive something like this

HTTP/1.1 302 found
Location: http://www.example.com/

Well, it tells the browser, to send the request/redirect to the given url and it does so, that’s how the http redirect happens but for more details, read URL redirection. Anyways, this is not about “How Http Redirect Works”, instead, it’s “WordPress Error : Headers Already Sent” and the simple solution is

function callback($buffer) {
    // You can modify $buffer here, and then return the updated code
    return $buffer;
}
function buffer_start() { ob_start("callback"); }
function buffer_end() { ob_end_flush(); }
// Add hooks for output buffering
add_action('init', 'buffer_start');
add_action('wp_footer', 'buffer_end');

Just put the code in the functions.php and the error will be gone. What it actually does is that, at the very beginning of WordPress boot up, it turns on the output buffering usinig add_action('init', 'buffer_start'), in other words, WordPress calls the buffer_start() function when init hook triggers (when WordPress loads it’s core) and this is just ob_start() to start output buffering and add_action('wp_footer', 'buffer_end') bound the WordPress to run buffer_end() function when wp_footer hook is processed and it happens when WordPress reaches the footer of the page where wp_footer() function triggers the hook. In a plain Php script someone may do this like



    
        

It's an example.

The ob_start() function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.

The contents of this internal buffer may be copied into a string variable using ob_get_contents(). To output what is stored in the internal buffer, use ob_end_flush(). Alternatively, ob_end_clean() will silently discard the buffer contents. Here, the callback callback function is optional but if it’s been used then someone can change the output before sending it to the browser, for example :



    
        

It's like comparing apples to oranges.

[/code] In the browser, the output will be

    
        

It's like comparing oranges to oranges.

Well, there are so many articles have been written about this including this nice answer on StacOverflow but still now I see question about this on StackOverflow, so I thought, lets write it once more here, maybe someone will find this one someday and could be helpful. Thanks!

Latest Blog

0
Php

PHP – 8.0 Match Expression

In PHP 8.0 there is a new feature or I should say a new language construct (keyword) going to be introduced,  which has been implemented depending […]

0
Php

New Union Type in PHP – 8

A new RFC has been finished voting and accepted for PHP – 8. That is “Union Types”, which is an extension of PHP’s type system. […]