I have extended WooCommerce to create a custom endpoint. This endpoint, points to a template that has a form to save user meta data. When the form is submitted I want it to redirect to another URL. Until I added add_action( 'init', array($this, 'ob_function'));
this was giving me a "headers already sent" error message. (ob_function
simply contains ob_start
).
My question is, should I be doing it this way? I've taken a look at some examples, have read up on it, etc but am unsure if this is bad practice (even though it works) as I guess that this is declaring ob_start
every time init
is loaded. I tried using conditional logic for the endpoint URL to restrict ob_start
being declared when this destination was the URL but kept receiving the aforementioned 'headers already sent' error message.
I have extended WooCommerce to create a custom endpoint. This endpoint, points to a template that has a form to save user meta data. When the form is submitted I want it to redirect to another URL. Until I added add_action( 'init', array($this, 'ob_function'));
this was giving me a "headers already sent" error message. (ob_function
simply contains ob_start
).
My question is, should I be doing it this way? I've taken a look at some examples, have read up on it, etc but am unsure if this is bad practice (even though it works) as I guess that this is declaring ob_start
every time init
is loaded. I tried using conditional logic for the endpoint URL to restrict ob_start
being declared when this destination was the URL but kept receiving the aforementioned 'headers already sent' error message.
2 Answers
Reset to default 2My question is, should I be doing it this way?
Yes, but only if the other possible/better fixes do not work for you.
You can also read this article about troubleshooting common WordPress errors such as the "headers already sent" error.
And when developing a plugin/theme, I suggest you to turn on WordPress debugging because normally, you would be able to identify the source of an error by (simply) checking the error_log
or wp-content/debug.log
file for entries/lines relevant to the error. :)
And here's a working example as a reference when adding custom WooCommerce endpoint:
Here's the code which adds a /company-profile
endpoint to the WooCommerce "My Account" endpoint; so you'd have example/my-account/company-profile
, which simply displays a form with a field to set/update the user's company ID (registration number..), and upon successful update, the user is sent back to the "My Account" page.
And if you look at that code and try it with a default WordPress installation — with just WooCommerce as the only active plugin, you should see that there's no output being generated prior to performing the redirection.
From what I can tell opinion is divided on how to handle this (although Tom Macfarlin - a reputable and well respected WordPress contributor more or less suggests to do what I've used to solve this.
For good measure, I added ob_flush();
after ob_start();
as I couldn't sensibly see that I should start an output buffer and leave it in that state.
A concern I mentioned was that add_action( 'init', array($this, 'ob_function'));
would call this every time init
was loaded. In the end I added this to ob_function()
so that it was only called when this endpoint was the URI and a $_POST variable was set:
public function ob_function(){
// Start output buffer to solve headers already sent issue for company profile redirect when saved.
if( 'https://' . $_SERVER['HTTP_HOST']. $_SERVER['REQUEST_URI'] == trailingslashit(get_site_url()) . 'recruiters/my-account/company-profile/' ){
ob_start();
ob_flush();
}
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745344709a4623489.html
ob_start()
for what you're trying to do. I don't see how it's relevant? Are you just doing it because it made the error go away? That's not what it's for. – Jacob Peattie Commented Jul 2, 2019 at 5:43ob_start()
is for capturing output, so as a side effect you're preventing any output from happening too early, but you haven't actually solved the cause of why you were getting output. I suggest forgetting aboutob_start()
and updating your question with relevant code for this initial problem, so you can get help with that. – Jacob Peattie Commented Jul 2, 2019 at 5:45ob_start()
as WordPress already generates the output that I think you're referring to i.e. It generates its own headers which is why I'm gettingheaders already sent
. The WooCommerce plugin does itself use something similar and I haven't included it for the sake of it. – NickC Commented Jul 2, 2019 at 7:26