export to csv wordpress

前端 未结 5 472
后悔当初
后悔当初 2020-12-24 04:13

I need to export data in one table in a csv file. I\'m able to get the data fine but the CSV file is not being generated by the browser.

My code is like this: its th

5条回答
  •  爱一瞬间的悲伤
    2020-12-24 04:50

    I met the same problem. I found that the problem is not the headers code, but the link you click to export. You need to add an argument named "noheader":

        Export 
    

    I replaced my code with exactly the same headers code mentioned in this question, and it does work as well. My export process code looks like:

    if( isset( $_GET['export'] ) ) {
    
        $csv = generate_csv();
    
        $filename = 'results.csv';
        $now = gmdate('D, d M Y H:i:s') . ' GMT';
    
        header( 'Content-Type: application/octet-stream' ); // tells browser to download
    
        header( 'Content-Disposition: attachment; filename="' . $filename .'"' );
        header( 'Pragma: no-cache' ); // no cache
        header( 'Expires: ' . $now ); // expire date
    
        echo $csv;
        exit;
    }
    

    The last two headers tell the browser not to cache the exported content.

提交回复
热议问题