正在尝试从WordPress前端导出到CSV。这是我到目前为止的情况。我可以从管理区域导出到CSV,但由于某些原因,我无法从前端导出。我做错了什么?
// Add action hook only if action=download_csv_sl
if ( isset($_GET[\'action\'] ) && $_GET[\'action\'] == \'download_csv_sl\' ) {
// Handle CSV Export
add_action( \'init\', \'csv_export_sl\');
}
function csv_export_sl() {
// Check if we are in WP-Admin
if( is_admin() ){ return false; }
// Nonce Check
$nonce = isset( $_GET[\'_wpnonce\'] ) ? $_GET[\'_wpnonce\'] : \'\';
if ( ! wp_verify_nonce( $nonce, \'download_csv_sl\' ) ) {
die( \'Security check error\' );
}
// Get Current Date and Time
date_default_timezone_set(\'America/Denver\');
$currentDate = date(\'Y_m_d-H_i_s\');
$domain = $_SERVER[\'SERVER_NAME\'];
$filename = \'Store_List-\' . $currentDate . \'.csv\';
// Get Quarter and Ranking from Shop Profiles
$getQuarter = do_shortcode(\'[usermeta key="current_quarter" user="717"]\');
if ($getQuarter == \'1\'){
$currentQuarter = \'ranker_q1\';
} elseif ($getQuarter == \'2\'){
$currentQuarter = \'ranker_q1_89\';
} elseif ($getQuarter == \'3\'){
$currentQuarter = \'ranker_q1_90\';
} elseif ($getQuarter == \'4\'){
$currentQuarter = \'ranker_q1_91\';
} else {
$currentQuarter = \'\';
}
// Name the columns in the spreadsheet
$header_row = array(
\'Store Number\',
\'Address\',
\'Franchisee\',
\'Q\'.$getQuarter.\' Ranking\'
);
$data_rows = array();
// Query users
$user_query = new WP_User_Query( array(
\'role__in\' => [\'shop\',\'shop_co\',\'shop_noco\'],
\'orderby\' => \'meta_value_num\',
\'order\' => \'ASC\',
) );
$users = $user_query->get_results();
foreach ( $users as $user ) {
if (!empty($user->Group)){
$row = array(
$user->Store_Number, // Get Store Number
$user->Address, // Get Address
$user->display_name, // Get Franchisee
$user->$currentQuarter // Get Ranking
);
$data_rows[] = $row;
}
}
$fh = @fopen( \'php://output\', \'w\' );
fprintf( $fh, chr(0xEF) . chr(0xBB) . chr(0xBF) );
header( \'Cache-Control: must-revalidate, post-check=0, pre-check=0\' );
header( \'Content-Description: File Transfer\' );
header( \'Content-Transfer-Encoding: UTF-8\' );
header( \'Content-type: text/csv\' );
header( "Content-Disposition: attachment; filename={$filename}" );
header( \'Expires: 0\' );
header( \'Pragma: public\' );
fputcsv( $fh, $header_row );
foreach ( $data_rows as $data_row ) {
fputcsv( $fh, $data_row );
}
fclose( $fh );
ob_end_flush();
die();
}
add_shortcode(\'bc_export\', \'csv_export_storelist_shortcode\');
function csv_export_storelist_shortcode() {
$url = $_GET[\'uri\'];
$nonce = wp_create_nonce( \'download_csv_sl\' );
$exportList = \'<a href="\'. $url . \'&action=download_csv_sl&_wpnonce=\'. $nonce .\'" class="page-title-action">Export to CSV</a>\';
return $exportList;
}
问题似乎在于
$url
在短代码内链接路径,但我不知道路径是什么。当前代码导致单击链接时找不到页面。