大家好,所有wordpress开发者:请帮我看看如何创建将导出csv的按钮。我在下面创建了一些函数作为csv输出,但我现在的问题是如何使用按钮或href调用此函数。用wordpress方式谢谢。
function csv(){
global $wpdb;
$sql = "SELECT * from activity WHERE activity_date = '2016-08-07' ";
$MyQuery = $wpdb->get_results($sql);
// Process report request
if (! $MyQuery) {
$Error = $wpdb->print_error();
die("The following error was found: $Error");
} else {
$csv_fields=array();
$csv_fields[] = 'Activity Date';
$csv_fields[] = 'Name';
$csv_fields[] = 'Activity Name';
$csv_fields[] = 'Email Address';
$csv_fields[] = 'Phone No.';
$output_filename = 'MyReport.csv';
$output_handle = fopen( 'php://output', 'w' );
header('Content-Disposition:attachment');
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename="'.$output_filename.'"' );
// Insert header row
fputcsv( $output_handle, $csv_fields );
foreach ($MyQuery as $Result) {
$leadArray = (array) $Result; // Cast the Object to an array
fputcsv( $output_handle, $leadArray );
}
exit;
}
}