I have some custom tables in a Wordpress database that I need to export to CSV. I created a dashboard widget to help with this based on some assistance given to me on on one of my SO questions. It basically consists of a single button that posts back and does some output buffering to render the CSV.
I've had it working correctly for a few weeks now, but when I went to push it live on our launch date, I ran into some odd behavior. The gist is that when I export locally on my dev box (Windows 7/Xampp) or on my personal hosted site (Linux) everything exports fine, but when I export on the client's server (Linux) the data is exported, but the CSV also contains the markup for the rest of the wp-admin dashboard page that follows after the widget is rendered. For example:
joe, blow
john, smith
<div><strong>Last entry: </strong>Hank Jones</div>
<div><strong>Total submissions: </strong> 7</div>
.. remaining markup removed for brevity
You can see that the CSV contains the <div><strong>Last entry:
markup (and a whole lot more). I have no idea why this would be different based on different servers, but I wanted to post my plugin code in hopes that someone can see what's going on (and tips for improvement if applicable).
<?php
/*
Plugin Name: My Plugin
Description: Exporting Stuff
Author: tjans
Version: 1.0
Author URI: http://example.com
*/
add_action('init', 'myplugin_export_buffer_start');
add_action('in_admin_footer', 'myplugin_export_buffer_end');
function myplugin_export_ob_callback($buffer) {
return $buffer;
}
function myplugin_export_buffer_start()
{
ob_start("myplugin_export_ob_callback");
}
function myplugin_export_buffer_end()
{
ob_end_flush();
}
function myplugin_display_mydata_export_widget_process()
{
$errors = array();
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && isset ( $_POST['myplugin_export_button'] ))
{
ob_clean(); // erase the output buffer...blow away all the code up to this point
global $wpdb;
$tablePrefix = $wpdb->prefix;
$qry = "QUERY TO PULL EXPORT DATA";
$result = $wpdb->get_results($qry, ARRAY_A);
if ($wpdb->num_rows)
{
$date = new DateTime();
$ts = $date->format("Y-m-d-G-i-s");
$filename = "mydata-export-$ts.csv";
header( 'Content-Type: text/csv' );
header( 'Content-Disposition: attachment;filename='.$filename);
$fp = fopen('php://output', 'w');
// show a header
$headRow = $result[0];
fputcsv($fp, array_keys($headRow));
foreach ($result as $data)
{
foreach($data as $field=>$value)
{
$data[$field] = stripslashes($value);
}
fputcsv($fp, $data);
}
fclose($fp);
$contLength = ob_get_length();
header( 'Content-Length: '.$contLength);
}
}
return myplugin_display_mydata_export_form_widget();
}
function myplugin_display_mydata_export_form_widget()
{
global $wpdb;
$tableName = $wpdb->prefix . "myplugin_mydata";
$submissionCount = $wpdb->get_var("select count(Id) from $tableName");
$lastUser = $wpdb->get_row("QUERY TO RETRIEVE LAST RECORD", ARRAY_A);
?>
<?php if($lastUser) { ?><div><strong>Last entry: </strong><?php echo $lastUser['FirstName'] ?> <?php echo $lastUser['LastName'] ?> (<?php echo date('F jS, Y - g:i a', $lastUser['DateStamp']) ?>)</div><?php } ?>
<div><strong>Total submissions: </strong> <?php echo $submissionCount ?></div>
<form id="myplugin_mydata_export_widget" method="post" action="">
<input type="submit" name="myplugin_export_button" value="Export All" />
</form>
<?php
}
function myplugin_add_mydata_export_dashboard_widgets()
{
// widget_id, widget_name, callback, control_callback
wp_add_dashboard_widget(
'myplugin-mydata-export-form-widget',
'Form Submissions',
'myplugin_display_mydata_export_widget_process'
);
}
add_action('wp_dashboard_setup', 'myplugin_add_mydata_export_dashboard_widgets' );