Last active
December 15, 2015 08:59
-
-
Save yoren/5234827 to your computer and use it in GitHub Desktop.
WordPress: Add link button to export posts into a Excel file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(document).ready(function($) { | |
if(typeof(pagenow) !== 'undefined' && pagenow == 'edit-post') { | |
$('h2 .add-new-h2').after('<a id="data_output" class="add-new-h2" href="?ay_output=excel" target="_blank">輸出 Excel 會員列表</a>'); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'admin_init', 'ay_output' ); | |
function ay_output() { | |
if ( isset( $_GET['ay_output'] ) && 'excel' == $_GET['ay_output'] && current_user_can( 'publish_posts' ) ) { | |
// Error reporting | |
error_reporting( E_ALL ); | |
//date_default_timezone_set( 'Asia/Taipei' ); | |
// PHPExcel | |
requay_once 'lib/PHPExcel.php'; | |
// Create new PHPExcel object | |
$objPHPExcel = new PHPExcel(); | |
$objPHPExcel->createSheet(); | |
// TODO: Loop to generate Rows, Columns, Cells | |
// set sheet0 as default when opening the excel | |
$objPHPExcel->setActiveSheetIndex( 0 ); | |
// Redirect output to a client’s web browser (Excel5) | |
header( 'Content-Type: application/vnd.ms-excel' ); | |
header( 'Content-Disposition: attachment;filename="data_' . date( 'Ymd' ) . '.xls"' ); | |
header( 'Cache-Control: max-age=0' ); | |
$objWriter = PHPExcel_IOFactory::createWriter( $objPHPExcel, 'Excel5' ); | |
$objWriter->save( 'php://output' ); | |
exit; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment