Nagłówki już wysłane na niestandardowej wtyczce (funkcja eksportu)
Opracowałem wtyczkę i mam funkcję eksportu, która eksportuje dane z wordpress do csv. Problem polega na tym, że instrukcja „Nagłówek” powoduje błąd „Nagłówki już wysłane”.
Sprawdziłem mój kod i nie ma żadnego kodu HTML ani niczego wysyłanego wcześniej.
błąd pochodzi z script-loader.php (wyjście rozpoczęło się w /home/test1/public_html/wp-includes/script-loader.php:1403)
Przyjrzałem się podstawowemu php i sekcja pokazuje, co następuje –
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;
}
Konkretna linia 1403 –
if ( !empty($wp_styles->print_html) )
echo $wp_styles->print_html;
oto moja funkcja, która konkretnie wykonuje eksport.
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;
}
Czy ktoś może mi w tym pomóc. Przeszukałem forum po forum, próbując wszystkiego, od ob_clean do ob_start, ob_flush itp.
dzięki Craigowi.