1

i trying to make excel download in front page (not admin pages) with call own wordpress plugin shortcode

here is my excel download code :

<?php
    global $wpdb;
    $item = get_items( $_GET['id'] );
    $product_info = $wpdb->get_row('SELECT * FROM '.$wpdb->prefix.'_table_product WHERE id="'.$item->prd_id.'"', ARRAY_A);

    /* PhpSpreadsheet Load */
    require dirname( __FILE__ ).'/phpspreadsheet/autoload.php';

    use PhpOffice\PhpSpreadsheet\Spreadsheet;
    use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
    // use PhpOffice\PhpSpreadsheet\IOFactory;

    /* Settings
     * url : https://phpspreadsheet.readthedocs.io/en/develop/topics/recipes/#write-a-newline-character-n-in-a-cell-altenter
     */
    \PhpOffice\PhpSpreadsheet\Cell\Cell::setValueBinder( new \PhpOffice\PhpSpreadsheet\Cell\AdvancedValueBinder() );

    /* Get Template Excel */
    $template_file = dirname(__FILE__) . '/template.xlsx';

    $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
    $spreadsheet = $reader->load($template_file);

    /* Write Value Current Registration Form Data */
    ... some set value codes ...


    /* Save Excel and Download */
    $spreadsheet->getActiveSheet()->setTitle('registration');
    $spreadsheet->setActiveSheetIndex(0);

    $reg_date = date('Ymd', strtotime($item->reg_date));
    $filename = $reg_date.'_'.$item->user_name;

    ob_start();

    // Redirect output to a client’s web browser (Xlsx)
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"');
    header('Cache-Control: max-age=0');
    // If you're serving to IE 9, then the following may be needed
//    header('Cache-Control: max-age=1');

    $writer = new Xlsx($spreadsheet);
    ob_get_clean();
    $writer->save('php://output');

?>

after download, and open file. but, didn't opened excel file with error messages like "it is not available excel type ..."

and open excel file using VSCODE excel files contents below :

<!DOCTYPE html>
<!--[if IE 9 ]><html lang="ko-KR" prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb#" class="no-js ie ie9"> <![endif]-->
<!--[if IE 10 ]><html lang="ko-KR" prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb#" class="no-js ie ie10"> <![endif]-->
<!--[if (gte IE 11)|!(IE)]><!--><html lang="ko-KR" prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb#" class="no-js"><!--<![endif]-->
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="naver-site-verification" content="dfc8906033b9ecd79b0a715f6e12e05589e78947"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

    <meta name="description" content="">
    <link rel="profile" href="http://gmpg.org/xfn/11">
    <link rel="pingback" href="https://test.com/xmlrpc.php">
    <link rel="stylesheet" href="https://test.com/wp-content/themes/testtheme/style.css">

    <title>TITLE</title>
...
▼ MORE Header Code
...

▼ BINARY CODE (it seems excel code) 
                PK

phpspreadsheet v1.6.0 (i guess)

anybody had this issues? and solving?

error page is this enter image description here

please help, experts. thanks.

  • Where do you put that download code? – Krzysiek Dróżdż Feb 18 at 8:42
  • @KrzysiekDróżd call shortcode page (not post, not admin) – Won-Ik Jang Feb 18 at 8:47
  • what do you mean by “shortcode page”? – Krzysiek Dróżdż Feb 18 at 8:47
  • @KrzysiekDróżdż it means just page (not in admin panel, for users). check the pickture – Won-Ik Jang Feb 18 at 8:49
0

OK, so you have some PHP code that will return binary data and you want to use it.

So if you want to return only that binary data, then you can't but it inside a page as a shortcode. Shortcodes are printed as article content, so all other parts of page will be printed also (header, navigation menus, and so on).

So there are 2 solutions:

  1. Changing the way you want to do this
  2. Cheating and using Output Buffering (but it's not a nice solution).

1. Printing the content earlier

The best solution would be to use template_redirect hook (or some other early hook) and printing all the contents in there.

The only problem is that you have to know, what file should be printed, so you'll have to put that info as meta values to the post, and not as a shortcode.

add_action( 'template_redirect', function () {
    if ( <CONDITION> ) { // check if excel file should be printed
        // put your excel printing code in here
    }
} );

2. Output buffering

The other way (but not very nice one) would be to start buffering before any content gets printed and discard anything that gets printed before the shortcode.

add_action( 'init', function () {
    ob_start();
} );

add_action( 'wp_footer', function () {
    ob_end_flush();
} );

And in your shortcode:

ob_end_clean();
// the rest of your code
  • thank you so much. i was trying. Which file should the add_action() function be located in? – Won-Ik Jang Feb 19 at 6:44
  • @Won-IkJang for example in functions.php or in one of files of your plugin. – Krzysiek Dróżdż Feb 19 at 6:46
  • it works! put in functions.php file. Thanks. no more stay overnight because of you! thank you so much! – Won-Ik Jang Feb 19 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.