admin_post_{$action}

最后更新于:2021-11-25 19:45:10

do_action( “admin_post_{$action}” )

Fires on an authenticated admin post request for the given action.

源文件

文件: gc-admin/admin-post.php

View on Trac

/**
 * Generate CSV File.
 */

add_action( 'admin_post_generate_csv', 'lunchbox_generate_orders_csv' );
function lunchbox_generate_orders_csv() {

	global $gcdb;

	$filename = 'lunchbox-orders';
	$generatedDate = $generatedDate = date('d-m-Y His');

	/**
	 * output header so that file is downloaded
	 * instead of open for reading.
	 */
	header("Pragma: public");
	header("Expires: 0");
	header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
	header("Cache-Control: private", false);
	header('Content-Type: text/csv; charset=utf-8');
	// header("Content-Type: application/octet-stream");
	header("Content-Disposition: attachment; filename="" . $filename . " " . $generatedDate . ".csv";" );
	// header('Content-Disposition: attachment; filename=lunchbox_orders.csv');
	header("Content-Transfer-Encoding: binary");

	/**
	 * create a file pointer connected to the output stream
	 * @var [type]
	 */
	$output = fopen('php://output', 'w');
	$results = $gcdb->get_results( "SELECT * FROM $gcdb->posts WHERE post_type = 'shop_order'", ARRAY_A );

	/**
	 * output the column headings
	 */
	fputcsv( $output, array('Order ID', 'Order Title', 'Order Date'));

	foreach ( $results as $key => $value ) {
		// $array[] = '';
		$modified_values = array( 
			$value['ID'],
			$value['post_title'],
			$value['post_date']
		);

		fputcsv( $output, $modified_values );
	}
	return $output;
}