我开发了一个插件,有一个导出功能,可以将数据从wordpress导出到csv。。问题是“Header”语句导致了“Headers ready sent error”。
我已经检查了我的代码,并没有html或任何要发送的东西。
错误来自脚本加载程序。php(输出开始于/home/test1/public\\u html/wp includes/script loader.php:1403)
我已经研究了核心php,本节显示了以下内容-
function _print_styles() {
global $compress_css;
$wp_styles = wp_styles();
$zip = $compress_css ? 1 : 0;
if ( $zip && defined(\'ENFORCE_GZIP\') && ENFORCE_GZIP )
$zip = \'gzip\';
if ( $concat = trim( $wp_styles->concat, \', \' ) ) {
$dir = $wp_styles->text_direction;
$ver = $wp_styles->default_version;
$concat = str_split( $concat, 128 );
$concat = \'load%5B%5D=\' . implode( \'&load%5B%5D=\', $concat );
$href = $wp_styles->base_url . "/wp-admin/load-styles.php?c={$zip}&dir={$dir}&" . $concat . \'&ver=\' . $ver;
echo "<link rel=\'stylesheet\' href=\'" . esc_attr($href) . "\' type=\'text/css\' media=\'all\' />\\n";
if ( !empty($wp_styles->print_code) ) {
echo "<style type=\'text/css\'>\\n";
echo $wp_styles->print_code;
echo "\\n</style>\\n";
}
}
if ( !empty($wp_styles->print_html) )
echo $wp_styles->print_html;
}
专用线1403-
if ( !empty($wp_styles->print_html) )
echo $wp_styles->print_html;
这是我的函数,专门用于导出。
function process_bulk_action()
{
global $wpdb;
$table_name = $wpdb->prefix . \'cj_raffle_tickets\'; // do not forget about tables prefix
if (\'delete\' === $this->current_action()) {
$ids = isset($_REQUEST[\'ticketid\']) ? $_REQUEST[\'ticketid\'] : array();
if (is_array($ids)) $ids = implode(\',\', $ids);
if (!empty($ids)) {
$wpdb->query("DELETE FROM $table_name WHERE ticketid IN($ids)");
}
}
if (\'export\' === $this->current_action()) {
//csv_export();
//ob_start();
$output = \'\'; //Assigning the variable to store all future CSV file\'s data
$result = $wpdb->get_results("SHOW COLUMNS FROM " . $table_name . ""); //Displays all COLUMN NAMES under \'Field\' column in records returned
if (count($result) > 0) {
foreach($result as $row) {
$output = $output . $row->Field . ",";
}
$output = substr($output, 0, -1); //Removing the last separator, because thats how CSVs work
}
$output .= "\\n";
$ids = isset($_REQUEST[\'ticketid\']) ? $_REQUEST[\'ticketid\'] : array();
if (is_array($ids)) $ids = implode(\',\', $ids);
if (!empty($ids)) {
$values = $wpdb->get_results("SELECT * FROM $table_name where ticketid IN ($ids)"); //This here
}
foreach ($values as $rowr) {
$fields = array_values((array) $rowr); //Getting rid of the keys and using numeric array to get values
$output .= implode(",", $fields); //Generating string with field separator
$output .= "\\n"; //Yeah...
}
// Download the file
$upload_dir = wp_upload_dir();
$filedir = $upload_dir[\'path\'];
$filename = \'ticketssold_\' . time() . \'.csv\';
if ( ! is_writable( $filedir ) ) {
wp_die( "<p>Uploads directory is not writable, can\'t save the file </p>", \'Directory not writable\' );
}
$handle = fopen( $filedir . \'/\' . $filename, \'w\' );
fwrite( $handle, $output );
fclose( $handle );
header( \'Content-Description: File Transfer\' );
header( \'Content-Type: application/octet-stream\' );
header( \'Content-Disposition: attachment; filename=\'.$filename );
header( \'Content-Transfer-Encoding: binary\' );
header( \'Expires: 0\' );
header( \'Cache-Control: must-revalidate\' );
header( \'Pragma: public\' );
header( \'Content-Length: \' . filesize( $filedir . \'/\' . $filename ) );
flush();
readfile( $filedir . \'/\' . $filename );
exit;
}
谁能帮我一下吗。。我搜索了一个又一个论坛,尝试了从ob\\u clean到ob\\u start、ob\\u flush等所有内容。
谢谢你。