This question already has an answer here:

I want to be able to click my "Export" button and have a text file downloaded. When I try this, I get the error: "Warning: Cannot modify header information - headers already sent ....". I have a submenu loaded with this code.

   add_submenu_page(null, 'eMail Maintenance', 'Export eMail list', PLCOA_ADMIN_PERMISSION, 'plcoa_email_export_post',   'plcoa_email_export_handler');

which triggers my function 'plcoa_email_export_handler'. I wanted to test my logic with a very simple export before adding the actual logic. The function that I wrote to handle the post of the page 'plcoa_email_export_post' is 'plcoa_email_export_handler'. This is it:

function plcoa_email_export_handler(){
  header('Content-type: text/plain');
  header('Content-Disposition: attachment;
        filename="email export"');
  $content = 'someone@gmail.com' . chr(10) . 'someoneelse@gmail.com';
  echo $content;

How do I fix this? Is there some way to flush the buffer? Many thanks,

marked as duplicate by fuxia Dec 22 '18 at 17:30

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Browse other questions tagged or ask your own question.