1

I am working on a checkout proces in Wordpress where if customer clicks on the checkout button he gets redirected to a payment provider.

Important to know:

  • I use a session in this proces.
  • I use PHP header() function to sent the customer to this location.

My problem: Now when you click the button, there is no redirect, but an error instead.

Warning: Cannot modify header information - headers already sent by (output started at /var/www/vhosts/website.com/httpdocs/wp-content/themes/template/header.php:24) in /var/www/vhosts/website.com/httpdocs/wp-content/themes/template/page-checkout.php on line 84

Line 84 is:

header( "Location: " . $payment->getPaymentUrl() );

So it looks like I need to do the redirect earlier, but I don't know how to do it. Has anyone any idea? Hook the init or send_headers action?

I have one solution at the moment which solves the problem, but I know that it is not the right one. The solution is placing ob_start() in the top of my header.php

Sam
  • 7,252
  • 16
  • 46
  • 65
Robbert
  • 1,270
  • 6
  • 30
  • 62
  • 1
    `header()` functions **must** be called before any HTML output has been sent to the browser. You'll need to place any `header()` calls before Wordpress starts sending HTML content. I can't see the rest of your code, so the best I can say is to move the `header()` call above any code you have that has an output – Matt May 20 '14 at 14:50

2 Answers2

0

headers are only sent before echoing any data, so use it before any echo, printf, etc.

prakhar19
  • 465
  • 4
  • 18
0

So the reason you are getting this error is because the response headers are already sent and you cannot update headers after the response. To redirect earlier you have two options:

  1. You can use the wp_redirect() function and do your redirect at runtime before the page renders.
  2. You can hook into the template redirect action and do your wp_redirect() there.

    function my_redirect() {
        exit( wp_redirect( 'url' ) );
    }
    add_action( 'template_redirect', 'my_redirect' );
    

Both methods hook into Wordpress' own redirect architecture. If you have issues with these methods, you may want to consider refactoring your code so that your redirect conditions are set up earlier in the stack.

For full disclosure, if you absolutely need to redirect after headers have been sent, you can always use a Javascript redirect after the page loads. You can simply add this to your php file (though I would recommend refactoring instead of this method):

function append_script() {
    echo '<script type="text/javascript">window.location.replace("url");</script>';
}
add_action('wp_head', 'append_script');

Again, I would suggest going a different route than this, but it will get the job done. You would want to place this code within your conditional for the redirect, not in the document scope of the functions file.

UPDATE:

After rereading your question a couple more times, it seems like you might be putting the PHP header function within the document on the button you are trying to make redirect. If that is the case, you should definitely not be using the header() function. This function specifically updates header information before the page renders. If the user is clicking a link, the page has already rendered and the response has already been parsed.

What you are trying to do is an event-based redirect. You either need to parse the URL you need and add it to an <a> tag or setup an onclick attribute for your button with the same URL:

<button onclick="window.location.replace('url')">Button Text</button>

The only way that I could see you using PHP on an event-based redirect (ie the button click) is if you sent the user to a handler to parse the request and send the user to the appropriate URL. Otherwise, you are stuck with the methods listed above.

Matthew R.
  • 4,332
  • 1
  • 24
  • 39
  • Thanks for your answer! But I think we still don't understand each other. I use this payment script where you see the header function in line 52. https://github.com/mollie/mollie-api-php/blob/master/examples/1-new-payment.php Hopefully you understand me better now. Thanks – Robbert May 21 '14 at 07:28