0

In my admin dashboard, i created a custom module for report filtering using custom queries. I need to add another functionality which is to export these results to CSV. I tried to create a very simple sample csv export.

ob_end_clean();
$output .= 'column 1'. ',';
$output .= 'column 2'. ',';

$output .="\n";
$output .='"data 1",';
$output .='"data 2",';
$output .="\n";

$file = "custom_table";
$filename = $file."_".date("Y-m-d_H-i",time());

header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");

echo $output;

Unfortunately, it produces an error.

Warning: Cannot modify header information - headers already sent by (output         started at C:\xampp\htdocs\rtlcbsasia\wp-admin\includes\template.php:1947) in C:\xampp\htdocs\rtlcbsasia\wp-content\plugins\wpdm-reports\wpdm-reports.php on line 38

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\rtlcbsasia\wp-admin\includes\template.php:1947) in C:\xampp\htdocs\rtlcbsasia\wp-content\plugins\wpdm-reports\wpdm-reports.php on line 39

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\rtlcbsasia\wp-admin\includes\template.php:1947) in C:\xampp\htdocs\rtlcbsasia\wp-content\plugins\wpdm-reports\wpdm-reports.php on line 40
column 1,column 2, "data 1","data 2",

Any help would be appreciated. Thank in advance

0

You can't use header after content has been sent to the browser, headers were already sent. Your code runs after part of the admin page it appears on has already been output to the browser. You can solve this by moving your output code into a function hooked to something that runs earlier, like admin_init, or use the admin_post action.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.