php - Is this a correct usage of ob_start() in my WordPress project?

I have extended WooCommerce to create a custom endpoint. This endpoint, points to a template that has a form to save use

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.

Share Improve this question asked Jul 2, 2019 at 5:41 NickCNickC 331 silver badge6 bronze badges 5
  • No it's not, and it's not clear to me from your description why you'd even want to use 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:43
  • 2 The "headers already sent" error means that your code is generating some sort of output too early. ob_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 about ob_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:45
  • @JacobPeattie I'm using ob_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 getting headers 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
  • Why do you need to redirect after WordPress sends headers? – Jacob Peattie Commented Jul 2, 2019 at 7:36
  • @JacobPeattie It doesn't really matter to me if the headers are sent or not but that's the error I get in any case. Tom Macfarlin - Resolving wp_redirect is essentially what I'm trying to solve. – NickC Commented Jul 2, 2019 at 10:35
Add a comment  | 

2 Answers 2

Reset to default 2

My 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

相关推荐

  • php - Is this a correct usage of ob_start() in my WordPress project?

    I have extended WooCommerce to create a custom endpoint. This endpoint, points to a template that has a form to save use

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信