0

I absolutely hate this error. And do not understand it at all.

So this is my code, what you want to focus on is the foreach loop:

public function reset_theme_options(){
    if($this->is_theme_options_array()){
        foreach($this->_theme_option['admin_options'] as $option_name=>$value){
            if($value != false){
                delete_option($option_name);
                $http = new AisisCore_Http_Http();
                wp_safe_redirect($http->get_current_url());
            }
        }
        $this->_update_option();
    }else{
        $this->_theme_option = array();
        $this->_update_option();
    }

}

Essentially all I am doing is getting the name of the options, so lets say you store all your options for your theme in 'My_theme', which is an array of settings.

All I do is take that and delete it - it works as is.

The problem is that I am then essentially (from wp_safe_redirect()) refreshing the page, I could do this through javascript and such but I think this way is better.

So everything works, I get my success message, the options are reset with the following hitch:

Warning: Cannot modify header information - headers already sent by (output started at /var/www/wordpress/wp-admin/menu-header.php:94) in /var/www/wordpress/wp-includes/pluggable.php on line 876

Ok so I click the reset button again and everything works as it should.

My steps in this error are:

(first start with a clean slate, no options saved) - save some options, hit submit. Then after the page refreshes, click reset, boom error, options still saved, click reset again, boom options reset.

Help?

2 Answers 2

1

When you redirect via wp_safe_redirect, this is done by sending a header to the browser containing the URL to redirect to. If any content has been sent to the browser before trying to send this header, you get the headers already sent error. Once content goes to the browser, happy header time is over, no more headers can be sent.

The solution is your code must run before any content goes to the browser, hook it to an action that executes before WordPress starts rendering the page.

2
  • any of those actions? - like hook to any action and then just call the action, because right now I call the method I showed when you hit submit.
    – TheWebs
    Commented Mar 13, 2013 at 0:44
  • 1
    but where is the function called exactly? menu-header.php outputs the admin menu, so page rendering has already begun. you need to hook an action before wp to do anything that may result in a redirect. you don't call the action, you hook your function to an action, and it gets executed when WordPress does that action.
    – Milo
    Commented Mar 13, 2013 at 1:21
0

The actual way to get around this is to put the following in your functions.php file:

function callback($buffer){
    return $buffer;
}

function add_ob_start(){
    ob_start("callback");
}

function flush_ob_end(){
    ob_end_flush();
}

add_action('init', 'add_ob_start');
add_action('wp_footer', 'flush_ob_end');
1
  • 1
    the actual way around it is to structure your code properly, this is how you get around it if you're doing things in the wrong places.
    – Milo
    Commented Mar 13, 2013 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.