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 :
*/
\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: fb: " class="no-js ie ie9"> <![endif]-->
<!--[if IE 10 ]><html lang="ko-KR" prefix="og: fb: " class="no-js ie ie10"> <![endif]-->
<!--[if (gte IE 11)|!(IE)]><!--><html lang="ko-KR" prefix="og: 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="">
<link rel="pingback" href=".php">
<link rel="stylesheet" href=".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
please help, experts. thanks.
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/xfn/11">
<link rel="pingback" href="https://test/xmlrpc.php">
<link rel="stylesheet" href="https://test/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
please help, experts. thanks.
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:
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
}
} );
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