Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wordpress csv export includes html in the output

Tags:

csv

wordpress

I am trying to dynamically create a downloadable csv file from within WordPress. The data to be included is correct, and the file is created for download successfully. However, the created csv file includes the template HTML output.

How can the data be output, without the HTML from the template?

Here is the function. The arrays are correctly populated with a sql query, but the results are identical to this.

function exportAsCSV (  ) {
  $csv = '';
  $header_array = array('Header 1', 'Header 2', 'Header 3');
  $data_array = array(
    array('Row 1 A', 'Row 1 B', 'Row 1 C'),
    array('Row 2 A', 'Row 2 B', 'Row 2 C'), 
  ); 

  $csv .= implode(', ', $header_array);
  $csv .= '\n';

  foreach($data_array as $row){
    $csv .= implode(', ', $row);
    $csv .= '\n';  
  } 


    $now = gmdate('D, d M Y H:i:s') . ' GMT';

    header('Content-Type: text/csv');
    header('Expires: ' . $now);

    header('Content-Disposition: attachment; filename="data.csv"');
    header('Pragma: no-cache'); 

    echo $csv; 
    exit();
}

The function is called as so:

if(isset($_GET['export_data'])){
  ob_end_clean();   
  exportAsCSV();
} 
like image 818
faerysteel Avatar asked Feb 17 '26 14:02

faerysteel


1 Answers

To make this safer, you could clear the output buffer before sending anything to it. After that, you can start sending data to the output buffer, and flush it before closing the data stream. Something like this:

    function get_csv($columns_headers, $data) {
        
        $filename = "php://output";

        ob_clean(); //empty the output buffer 
        ob_start();  //and start a new one
        
        header('Pragma: public');
        header('Expires: 0' );
        header('Cache-Control: no-cache, must-revalidate');
        header('Content-Type: text/csv');
        header('Content-Disposition: attachment; filename="'.'data.csv"');

        $outstream = fopen($filename, 'r+')  or die("Error opening php://output");
        
        fputcsv( $outstream , $columns_headers );

        foreach($data as $item)
            fputcsv($outstream,$item);
        
        ob_flush();  //send the whole buffer
        fclose($outstream) or die("Error closing php://output");    
        //all done
        die();

    }
like image 123
gonetil Avatar answered Feb 20 '26 19:02

gonetil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!