Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
wordpress template code for CSV export based on https://www.gloomycorner.com/export-data-to-csv-in-wordpress-with-php/
<?php /*template name: Export*/
if ( !is_user_logged_in() ) {
auth_redirect();
}
$admin_user = current_user_can('manage_options');
if (!$admin_user) die("ACCESS DENIED");
if (isset($_GET['export'])) {
$table_head = array('column1', 'column2', 'column3');
$table_body = array(
array('a', 'b', 'c'),
array('d', 'e', 'f')
);
$csv = implode( $table_head, ',' );
$csv .= "\n";
foreach($table_body as $row) {
$csv .= implode($row, ',' );
$csv .= "\n";
}
$filename = 'table.csv';
header( 'Content-Type: text/csv' );
header( 'Content-Disposition: attachment; filename="' . $filename .'"' );
header( 'Pragma: no-cache' );
header( "Expires: Sat, 26 Jul 1997 05:00:00 GMT" );
echo "xEFxBBxBF"; // UTF-8 BOM
echo $csv;
exit;
}
global $wpdb;
?>
<h1>Export</h1>
<a href="/export?export=table">Export</a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment