最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - code is working properly in Core PHP but writing coding in WordPress

programmeradmin0浏览0评论
function generate_pdf($array)
{
    header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="sample.csv"');

$fp = fopen('php://output', 'wb');
foreach ( $array as $line ) {
    $val = explode(",", $line);
    fputcsv($fp, $val);
}
fclose($fp);
}

$array = array(
    'aaa,bbb,ccc,dddd',
    '123,456,789',
    '"aaa","bbb"'
);

generate_pdf($array);

Here's my code generating correct csv file, But When I'm implementing this in my Custom Plugin It's generating Coding in .csv file

function generate_pdf($array)
{
    header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="sample.csv"');

$fp = fopen('php://output', 'wb');
foreach ( $array as $line ) {
    $val = explode(",", $line);
    fputcsv($fp, $val);
}
fclose($fp);
}

$array = array(
    'aaa,bbb,ccc,dddd',
    '123,456,789',
    '"aaa","bbb"'
);

generate_pdf($array);

Here's my code generating correct csv file, But When I'm implementing this in my Custom Plugin It's generating Coding in .csv file

Share Improve this question edited Oct 15, 2020 at 18:41 Tom J Nowell 61k7 gold badges79 silver badges148 bronze badges asked Oct 15, 2020 at 18:05 Mr_UssiMr_Ussi 363 bronze badges 2
  • 1 Can you rephrase your last sentence? It says your code X but when in your custom plugin it also does X which doesn't make sense, what do you mean by generate coding in CSV file? How are you using the code in your plugin? it isn't clear what the WP version of this looks like or how it misbehaves – Tom J Nowell Commented Oct 15, 2020 at 18:42
  • I think I’ve seen this before... instead of outputting just the CSV or PDF you’re also getting HTML in the output, correct? If so then you’ll need ob_start() at the beginning. Let me see if I can find my solution. – Tony Djukic Commented Oct 15, 2020 at 19:36
Add a comment  | 

1 Answer 1

Reset to default 0

When I attempted something similar I experienced the same issue where the HTML code of the page I was generating the CSV from would be included within the CSV mixed with the data I actually wanted.

The two things I figured out, after searching and reading and reading and searching was to use ob_end_clean(); before I start outputting the content of the CSV. ob_end_clean() is 'Output Buffers End/Clean` - https://www.php/manual/en/function.ob-end-clean.php

I also added die() at the end of the CSV output code to, well, exit the process. You can also use exit().

function generate_pdf( $array ) {
    ob_end_clean(); //ADD THIS LINE
    header( 'Content-Type: text/csv' );
    header('Content-Disposition: attachment; filename="sample.csv"');

    $fp = fopen( 'php://output', 'wb' );
    foreach( $array as $line ) {
        $val = explode( ',', $line );
        fputcsv( $fp, $val );
    }
    fclose( $fp );
    die(); //add this as well
}
发布评论

评论列表(0)

  1. 暂无评论