hooks - PHP header() gives headers already sent

admin2025-06-03  0

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/httpdocs/wp-content/themes/template/header.php:24) in /var/www/vhosts/website/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

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/httpdocs/wp-content/themes/template/header.php:24) in /var/www/vhosts/website/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

Share Improve this question edited May 20, 2014 at 14:51 Pieter Goosen 55.5k23 gold badges117 silver badges211 bronze badges asked May 20, 2014 at 14:45 RobbertRobbert 1,2756 gold badges23 silver badges47 bronze badges 1
  • Where is your code? Please post it in context. – s_ha_dum Commented May 20, 2014 at 15:10
Add a comment  | 

1 Answer 1

Reset to default 0

I had to do it with WordPress's inbuilt redirect function; wp_redirect();

wp_redirect( $payment->getPaymentUrl() );
exit;
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748881342a314499.html

最新回复(0)