1

i have a page template thts sole purpose is to redirect to the url present in the customfield named extlink. but i keep getting the error "Warning: Cannot modify header information - headers already sent by (output started...." when i assign it to the post without assigning it working perfectly.

<?php
/**
 * Template Name: change
 **/

get_header();
?>

<?php
$redirect_url=get_field('extlink'); 
wp_redirect($redirect_url);
get_sidebar();
get_footer();
?>
user3449454
  • 61
  • 2
  • 12

1 Answers1

0

Your problem here is that the headers (content on the page) has already been given to the browser when you are trying to redirect the user, and you can't do that.

You need to use output buffers, which essentially capture the content of your whole page, allow any redirections or whatever happen first, and then display the content to the user, which will fix your script.

So just add this at the very top of the page from which you require this bit of code:

<?php
ob_start();
?>

And at the very bottom:

<?php
ob_end_flush();
?>
Lucas
  • 16,930
  • 31
  • 110
  • 182