admin管理员组文章数量:1302584
I have some code, implemented by a shortcode within a custom plugin. The code generates a CSV file from some data tables. When I run the shortcode within the admin section, it works perfectly. BUT when I attempt to run it on the front end, it fills the top of the CSV file with the pages HTML data such as:
<?DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
.......
# ID Product Price
1 2a keys 10.00
2 2b doors 55.00
The table of data does export, as it did in the admin section of the plugin. Just when the shortcode to the function is placed on the front end, the results is HTML code at the top of the file (as seen above). Can not for the life of me figure out how to stop this. The goal is to be able to have the user output, in CSV format, all their purchases. Here is the code:
function ShowResults($Name)
{
$rows = mmd_GetProducts($Name);
ob_start();
ob_end_flush();
header('Pragma: public');
header( 'Expires: 0' );
header( 'Cache-Control: private', false );
header( 'Content-Type: text/csv');
header( 'Content-Disposition: attachment; filename="'. 'Results.csv"');
$fp = fopen('php://output', 'w');
$header_row = array(
0 => '#',
1 => 'ID',
2 => 'Product',
3 => 'Price',
);
fputcsv($fp, $header_row);
foreach($rows as $Record)
{
if($Record['bDoNotDisplay'] == 1 )
continue;
$OutputRecord = array($cnt
$Record['id'],
$Record['product'],
$Record['price']);
$cnt ++;
fputcsv($fp, $OutputRecord);
}
unset($rows);
fclose( $fp );
ob_end_clean();
exit();
}
UPDATE BUT STILL A PROBLEM
Similar issue, except now the HTML code is at the bottom of the file. I changed the code to use add_action instead of calling a short code. I use this wordpress action call and then detect if this is the page that is loading
add_action( 'template_redirect', 'ShowResults', 10);
function ShowResults()
{
global $post;
$title = get_the_title($post);
if($title == "Export")
{
OutputCSV();
}
}
function OutputCSV()
{
$rows = mmd_GetProducts($Name);
ob_start();
ob_end_flush();
header('Pragma: public');
header( 'Expires: 0' );
header( 'Cache-Control: private', false );
header( 'Content-Type: text/csv');
header( 'Content-Disposition: attachment; filename="'. 'Results.csv"');
$fp = fopen('php://output', 'w');
$header_row = array(
0 => '#',
1 => 'ID',
2 => 'Product',
3 => 'Price',
);
fputcsv($fp, $header_row);
foreach($rows as $Record)
{
if($Record['bDoNotDisplay'] == 1 )
continue;
$OutputRecord = array($cnt
$Record['id'],
$Record['product'],
$Record['price']);
$cnt ++;
fputcsv($fp, $OutputRecord);
}
unset($rows);
fclose( $fp );
ob_end_clean();
exit();
}
Now the HTML code appears at the bottom of the file and not the top. Any suggestions?
I have some code, implemented by a shortcode within a custom plugin. The code generates a CSV file from some data tables. When I run the shortcode within the admin section, it works perfectly. BUT when I attempt to run it on the front end, it fills the top of the CSV file with the pages HTML data such as:
<?DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
.......
# ID Product Price
1 2a keys 10.00
2 2b doors 55.00
The table of data does export, as it did in the admin section of the plugin. Just when the shortcode to the function is placed on the front end, the results is HTML code at the top of the file (as seen above). Can not for the life of me figure out how to stop this. The goal is to be able to have the user output, in CSV format, all their purchases. Here is the code:
function ShowResults($Name)
{
$rows = mmd_GetProducts($Name);
ob_start();
ob_end_flush();
header('Pragma: public');
header( 'Expires: 0' );
header( 'Cache-Control: private', false );
header( 'Content-Type: text/csv');
header( 'Content-Disposition: attachment; filename="'. 'Results.csv"');
$fp = fopen('php://output', 'w');
$header_row = array(
0 => '#',
1 => 'ID',
2 => 'Product',
3 => 'Price',
);
fputcsv($fp, $header_row);
foreach($rows as $Record)
{
if($Record['bDoNotDisplay'] == 1 )
continue;
$OutputRecord = array($cnt
$Record['id'],
$Record['product'],
$Record['price']);
$cnt ++;
fputcsv($fp, $OutputRecord);
}
unset($rows);
fclose( $fp );
ob_end_clean();
exit();
}
UPDATE BUT STILL A PROBLEM
Similar issue, except now the HTML code is at the bottom of the file. I changed the code to use add_action instead of calling a short code. I use this wordpress action call and then detect if this is the page that is loading
add_action( 'template_redirect', 'ShowResults', 10);
function ShowResults()
{
global $post;
$title = get_the_title($post);
if($title == "Export")
{
OutputCSV();
}
}
function OutputCSV()
{
$rows = mmd_GetProducts($Name);
ob_start();
ob_end_flush();
header('Pragma: public');
header( 'Expires: 0' );
header( 'Cache-Control: private', false );
header( 'Content-Type: text/csv');
header( 'Content-Disposition: attachment; filename="'. 'Results.csv"');
$fp = fopen('php://output', 'w');
$header_row = array(
0 => '#',
1 => 'ID',
2 => 'Product',
3 => 'Price',
);
fputcsv($fp, $header_row);
foreach($rows as $Record)
{
if($Record['bDoNotDisplay'] == 1 )
continue;
$OutputRecord = array($cnt
$Record['id'],
$Record['product'],
$Record['price']);
$cnt ++;
fputcsv($fp, $OutputRecord);
}
unset($rows);
fclose( $fp );
ob_end_clean();
exit();
}
Now the HTML code appears at the bottom of the file and not the top. Any suggestions?
Share Improve this question edited Mar 3, 2021 at 17:53 Debbie Kurth asked Mar 1, 2021 at 19:41 Debbie KurthDebbie Kurth 4323 silver badges14 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 1Well this was a journey! The solution was 'die' and not exit AND use the add_action call that resulted in a notification PRIOR to any header call being called. The die forced the system to quit before attempting to write out the headers at the end of file.
The final code that works:
add_action( 'template_redirect', 'ShowResults', 10);
function ShowResults($Name)
{
global $post;
$title = get_the_title($post);
if($title == "Export")
OutputCSV();
}
function ExportCSV()
{
$rows = mmd_GetProducts($Name);
header('Pragma: public');
header( 'Expires: 0' );
header( 'Cache-Control: private', false );
header( 'Content-Type: text/csv');
header( 'Content-Disposition: attachment; filename="'. 'Results.csv"');
$fp = fopen('php://output', 'w');
$header_row = array(
0 => '#',
1 => 'ID',
2 => 'Product',
3 => 'Price',
);
fputcsv($fp, $header_row);
foreach($rows as $Record)
{
if($Record['bDoNotDisplay'] == 1 )
continue;
$OutputRecord = array($cnt
$Record['id'],
$Record['product'],
$Record['price']);
$cnt ++;
fputcsv($fp, $OutputRecord);
}
unset($rows);
fclose( $fp );
die; <<<=========== die NOT exit.
}
本文标签: databaseExporting table to csv works in the admin but exports HTML data when used on the front end
版权声明:本文标题:database - Exporting table to csv works in the admin but exports HTML data when used on the front end 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741708307a2393690.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
template_redirect
hook – birgire Commented Mar 3, 2021 at 15:48