I have made a custom plugin, which exports MYSQL table contents into a .csv file which gets saved in to wp uploads directory. Now, I am trying to give a download link for downloading directly a .csv file. But, I am getting this error "Warning: Cannot modify header information - headers already sent"

I'm so very grateful for all of the help you all give. I've searched for this topic and couldn't find it, if this topic is already answered here, please comment with the link to the question with the answer.


Code Which I tried for Downloaing:

header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
header( "Content-Length: " . strlen( $out ) );

// Output to browser with appropriate mime type, you choose ;)
header( "Content-type: text/x-csv" );
header( "Content-Disposition: attachment; filename=$filename" );

echo $out;
  • 1
    Have you seen this? – fuxia Jun 24 '13 at 7:31
  • Where are you putting this code? It won't work after any HTML has been sent to the browser. – shea Jun 24 '13 at 9:36
  • Now i have used the link as suggested by toscho by using it the errors are eliminated but download is not happening.I am just giving the link to download in the page where i want to download. – Abusufiyan Shaikh Jun 24 '13 at 9:44
  • The answer toscho linked to is exactly what you're after, what part is causing confusion? – TheDeadMedic Jun 24 '13 at 10:32
  • In add_action( 'admin_post_print.csv', 'print_csv' ); we are passing "admin_post_print.csv" as a name of action and what action we are specifying is "action=print.csv" in href. So i am confused in both of these. Can you please clear my confusion. Thanks – Abusufiyan Shaikh Jun 24 '13 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.