43

I'm encountering this error. and I have no idea dealing with this.

Cannot modify header information - headers already sent by (output started at /home/ben213/public_html/wp-content/themes/Bendaggers/functions.php:9) in /home/ben213/public_html/wp-includes/pluggable.php on line 934

my Functions.php file line # 9 is:

<?php if(function_exists('register_sidebar'))register_sidebar();?>

while my pluggable.php # 934 is

function wp_redirect($location, $status = 302) {
    global $is_IIS;

    $location = apply_filters('wp_redirect', $location, $status);
    $status = apply_filters('wp_redirect_status', $status, $location);

    if ( !$location ) // allows the wp_redirect filter to cancel a redirect
        return false;

    $location = wp_sanitize_redirect($location);

    if ( !$is_IIS && php_sapi_name() != 'cgi-fcgi' )
        status_header($status); // This causes problems on IIS and some FastCGI setups

    header("Location: $location", true, $status);}
endif;

I'm having a hard time figuring this out since im not a programmer. what seems to be wrong? kindly help me please...

Maxime
  • 8,645
  • 5
  • 50
  • 53
Ben Daggers
  • 1,000
  • 1
  • 16
  • 51
  • Hi Paul, Good day! I'm sorry but can you please translate that in english? so how am i gonna do that? – Ben Daggers Sep 11 '11 at 22:29
  • What is `pluggable.php`? Why do you have it? It looks like it should be included before `functions.php` as it tries to set HTTP headers, and these need to be set before you start outputting HTML. – Paul Grime Sep 11 '11 at 22:32
  • Got no idea here Paul, all i know is is for widgetizing my sidebar. have no idea, sorry. – Ben Daggers Sep 11 '11 at 22:37

2 Answers2

89

Your theme is printing output (text) to the browser, but then for some reason WordPress is redirecting the user (with wp_redirect) away from that page before the whole page is rendered. You can't start printing output and then redirect, or you'll get the error you see. That's what Paul Grime was getting at in his comment.

Ken White commented with a reference to a post with a similar problem. I've fixed this in my own experience by buffering the output of the script.

In your theme's functions.php file (which gets included every time your theme's pages load), put the following:

//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();
}

Now, even if part of your theme starts to send input to the browser, PHP won't send that text until the page is fully loaded, which allows WordPress to redirect users, if necessary, as part of its own logic.

Jasdeep Khalsa
  • 6,740
  • 8
  • 38
  • 58
hardy101
  • 1,311
  • 10
  • 5
  • 1
    @BenDaggers Please mark this answer as correct or comment on why it wasn't. Thanks. – hitautodestruct Nov 12 '12 at 09:54
  • 12
    Does this have any side effects? – papaiatis Mar 14 '13 at 14:25
  • 5
    Yes, this has side effects, memory usage is the most obvious, slower site response since the whole page must render before anything is sent. – Peter Wooster Aug 17 '13 at 11:52
  • Bloody cool! I had a `wp_redirect()` issue with [this plugin](https://github.com/jkudish/WordPress-GitHub-Plugin-Updater) (authorizing a GitHub app), and this did the trick. Surely, it's only a workaround, but nonetheless, quite effective. – brasofilo Aug 27 '13 at 14:10
  • Man! Thank you, I was wasting hours trying to do a simple redirect within my custom plugin! I felt like a completely dumb – Luiz Eduardo Sep 03 '15 at 07:46
  • Hi ! works great, but now, i have a new problem, My csv is downloading, thx to your solution, but it contains all the html code from the template, not the array that it should send. Do you know how to take only the array? Thx JC – jcdarocha Jul 12 '17 at 17:11
  • This wasn't working for me. I had to put directly @ob_start(); at the begining of function.php – Kaizoku Gambare Oct 06 '17 at 09:45
  • Perfect explanation. If you have got two php tags opened with empty lines in between, you will get this error. Put commented lines in between is the best solution. – Alaksandar Jesus Gene Dec 03 '17 at 12:11
  • To minimize performance effects and reduce time response when using ob_start() **it is better to hook the function just before sending page header** ([get_header](https://codex.wordpress.org/Plugin_API/Action_Reference/get_header) hook instead of init) and using [conditional tags](https://codex.wordpress.org/Conditional_Tags). – skotperez Feb 12 '19 at 21:49
  • Wow working for me – Mohamed Slimane Mar 21 '21 at 21:26
21

If you are trying to redirect to another page from your current page, where you have impose a condition or without condition, then use this code. E.gs you have two pages A.php, & B.php and currenty you are in A.php where you want to go to other page B.php on clicking the BUTTON.

   if(isset($_POST['save_btn']))
    {
        //write some of your code here, if necessary
        echo'<script> window.location="B.php"; </script> ';
     }
mega6382
  • 9,211
  • 17
  • 48
  • 69
Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81