Skip to content

Instantly share code, notes, and snippets.

@Dare-NZ
Created May 9, 2013 00:52
Show Gist options
  • Save Dare-NZ/5544790 to your computer and use it in GitHub Desktop.
Save Dare-NZ/5544790 to your computer and use it in GitHub Desktop.
A php file that takes a WP table and saves as a CSV
<?php
require_once('../../../../wp-load.php');
global $wpdb;
$wpdb->show_errors();
$table_name = $wpdb->prefix . "arg_signups";
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
newsletter_install();
}
$signups = $wpdb->get_results("SELECT * FROM `$table_name` ORDER BY id");
$header = "id\tname\temail\t";
$data = '';
if(!empty($signups)) {
foreach ($signups as $signup) {
$line = "\"" . $signup->id . "\"\t";
$line .= "\"" . $signup->name . "\"\t";
$line .= "\"" . $signup->email . "\"\t";
$data .= $line . "\n";
}
} else {
$data = "\n(0) Records Found!\n";
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=signups.csv");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment