0

I want my webpage to redirect to another webpage once some code is run by a user action. However, I am coding PHP on a Wordpress page (with php plugin) and I keep getting the error that the headers are already sent. Even if I try ob_start approach I still get this error. Does anyone know a workaround to redirecting after a user clicks a button on the webpage and a query is run considering that the headers have already been sent by the wordpress code?

The error message isWarning: Cannot modify header information – headers already sent by (output started at...

//the code currently
header("Location: http://example.com/full/?wid=$the_WID");
jwalsh
  • 11
  • 6

1 Answers1

0

PHP header() function must be called before send ANY type of data (except another headers) at browser and ob_start too if you wish to capture ALL trafic sent to client browser!!

Have you tried to write a javascript that makes the redirection?

<script type="text/javascript">
    window.location.href = "http://example.com/full/?wid=<?= $the_WID ?>";
</script>

Possibly Wordpress API let you do that, but I don't know how works that API.

Good luck!

OscarGarcia
  • 1,995
  • 16
  • 17
  • Actually this approach works, except that it will not capture the variable value $the_WID for the ID. You can't tack on a PHP variable to the javascript string? Anyway to make the PHP variable into a javascript variable. – jwalsh Feb 27 '15 at 02:00
  • Yes! You can! If is it pure PHP you can use `= $variable ?>` or `` but I don't know if there will problems with variable scope, so you can try this too: `` – OscarGarcia Feb 27 '15 at 07:06