Fixed: Cannot modify header information – headers already sent in WordPress

Advertisement

This warning or error is commonly seen in PHP redirection code or adding header PHP function, the most common issue is by accidentally adding spacing in both closing and starting PHP tag, another issue is by outputting text and adding header function below it, that also lead to a warning.

There are short work around on this in the net, we can also try using HTML alternative code for easy fix like by adding meta tag, but it’s kinda like unprofessional way in fact it is, so for the sake of this tutorial I’ll give you most easiest solution by simple adding this snippet into your current theme functions.php file.


/* Allow redirection
even if my theme starts to send output to the browser
================================================== */
add_action( 'init', 'do_output_buffer' );
function do_output_buffer() {
    ob_start();
}

That’s it, now PHP read both text and header even you’re adding header right after the text is sent for http request.

Advertisement