Contents
☰In WordPress plugin development, sometimes you may need to export data to a csv file. Here we share how to implement it with PHP code.
How to export a csv file with PHP code
header()
is used to send a raw HTTP header. To export a generated csv file, use below headers to tell the browser display a save dialog.
header( 'Content-Type: text/csv' ); // Supply the mime type
header( 'Content-Disposition: attachment; filename="downloaded.pdf"' ); // Supply a file name to save
Note:
There is not official
RFC
document for CSV files.text/csv
is not a standard mime type, but it is more clear and works fine.application/octet-stream
can also be used for csv files. However it is a very generous and it does not hint which application should be used to open the file.
Use two more headers to tell the content should not be cached by browser or any caches between the server and the browser.
header( "Cache-Control: no-cache, must-revalidate" );
header( "Expires: Sat, 26 Jul 1997 05:00:00 GMT" ); // Date in the past
Then echo the generated csv content directly:
echo $csv;
exit;
Note:
Any actual content must be output after
header()
. One common error is outputting content beforeheader()
byinclude
orrequire
a file. See more about header().
Below is the full example.
Example: Export data to csv in WordPress
Add an export button:
<a href="admin.php?page=export_example&export=table&noheader=1">Export</a>
Don’t forget add the argument named “noheader”. Otherwise, the csv content will directly be printed and there will be no Save dialog box popping up in the browser.
Process the export in the same file:
<?php
$table_head = array( 'column1', 'column2', 'column3' );
$table_body = array(
array( 'a', 'b', 'c' ),
array( 'd', 'e', 'f' )
);
// Process export
if( isset( $_GET['export'] ) ) {
$csv = implode( $table_head, ',' );
$csv .= "n"; // important! Make sure to use use double quotation marks.
foreach( table_body as row ) {
$csv .= implode( $row, ',' );
$csv .= "n";
}
$filename = 'table.csv';
header( 'Content-Type: text/csv' ); // tells browser to download
header( 'Content-Disposition: attachment; filename="' . $filename .'"' );
header( 'Pragma: no-cache' ); // no cache
header( "Expires: Sat, 26 Jul 1997 05:00:00 GMT" ); // expire date
echo $csv;
exit;
}
?>
<a href="admin.php?page=export_example&export=table&noheader=1">Export</a>
This example works well on Firefox, IE, Chrome.
Export data with Unicode characters to utf-8 bom csv
If your data contains Unicode characters, you may want to export a utf-8 bom csv that Excel will read properly. To achieve that, just output the BOM mark before data:
echo "xEFxBBxBF"; // UTF-8 BOM
echo $csv;
exit;
Hi there, thank you for the above. I tried using this example snippet However, I get the following error after clicking the link:
“Sorry, you are not allowed to access this page.”
The URL on the button is:
http://domain.com/wp-admin/admin.php?page=export_example&export=table&noheader=1
Is the above solution suitable for the WP admin area? Or just for the front end?