0

I have a client website with a custom-developed plugin that when data is updated (this is all in Wordpress admin backend) uses a PHP header('Location: ') function to switch to a different URL. I had originally developed in an environment with an old theme that I eventually scrapped for a newer better one, but now PHP throws an error saying:

PHP Warning: Cannot modify header information - headers already sent by (output started at /wp-admin/includes/template.php:2018) in /wp-content/plugins/*my-plugin-name*/save-data.php on line 47

which is where my header() function is in my plugin.

I originally was convinced it was a plugin conflict, but then I realized it was doing with the twenty fifteen theme and then any other theme I tried other than the crappy old one that was the active theme when I developed the plugin.

  • 1
    When is your function called exactly? Refer to the Action Reference to see the order of admin actions. You can't reliably set headers later than maybe send_headers. – Milo Jun 28 '17 at 18:26
  • @Milo The send_headers hook didn't work either so it must be further along in the action order than the HTTP headers. Is there a way I can detect what action load stage the code is currently at? – Talk Nerdy To Me Jun 28 '17 at 19:17
  • @Milo I tried detecting if the send_headers action had fired yet using if(did_action('send_headers')) and it returned that it did not. So now I'm at a loss. – Talk Nerdy To Me Jun 28 '17 at 19:28
  • 1
    Line 2018 of /wp-admin/includes/template.php is where the admin pages starts outputting html. Your plugin has to do whatever redirection it needs to do before that happens. I'm not sure what you mean by send_headers hook not working. It seems like your plugin is trying to set a header within the body of an admin page. Your whole saving logic has to be hooked to happen earlier. – Milo Jun 28 '17 at 19:57
  • @Milo Sorry for not being clear. I meant that you suggested trying send_headers and that didn't work and when I tried seeing if that action had fired at the point when my code tries to send a header, it returned that it had not fired yet, which is confusing to me why it isn't working. – Talk Nerdy To Me Jun 28 '17 at 21:49
0

You see that error because something is sending output (even space characters) before the header-location command. The error message tells you where the output is coming from. To fix, you'd have to edit that file - not a wise choice to edit core files, because the next update will overwrite your changes.

Assuming your plugin has a need to redirect, then you should use the wp_redirect() function (see here https://developer.wordpress.org/reference/functions/wp_redirect/ ).

Remember to put a exit; after the wp_redirect() to ensure any code following the wp_redirect is not executed - as the function's docs explain.

  • Thank you, Rick; I will be more specific. At the point in my code when my header() or wp_redirect() function hits, I did a check to see if Wordpress had performed the send_headers action (see this page for the load order I'm referring to that @Milo mentioned above) and it returned false. So even though Wordpress was telling me that the send_headers action hadn't fired yet, I still could not send an HTTP header. Any thoughts on that? – Talk Nerdy To Me Jun 29 '17 at 15:54
  • if you want to see what is sent (or recieved by the browser) before the part of the template that dies, put a die(); right before that point (probably before the point that the error message indicates). Then look at the page source to see all the stuff that gets loaded prior to the die(). Even a space character inadvertantly sent by a php file will cause the error message (like when you put a space character before the <?php command). – Rick Hellewell Jun 29 '17 at 22:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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