已在自定义插件上发送的标头(导出功能)

时间:2017-11-30 作者:Craig Johnstone

我开发了一个插件,有一个导出功能,可以将数据从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等所有内容。

谢谢你。

1 个回复
SO网友:kierzniak

您可能正在执行process_bulk_action 在已发送一些html和标头的模板中使用函数。你必须使用handle_bulk_actions-edit-post 钩子在发送任何内容之前执行代码。这可能是这样的:

function wpse_287406_register_export_option($bulk_actions) {

    $bulk_actions[\'export_csv\'] = __( \'Export to CSV\');

    return $bulk_actions;
}

add_filter( \'bulk_actions-edit-post\', \'wpse_287406_register_export_option\' );

function wpse_287406_export_csv( $redirect_to, $doaction, $post_ids ) {

    if ( $doaction !== \'export_csv\' ) {

        return $redirect_to;
    }

    // Export CSV code

    $file = realpath( dirname( __FILE__ ) . \'/file.csv\' );

    header( \'Content-Description: File Transfer\' );
    header( \'Content-Type: application/octet-stream\' );
    header( \'Content-Disposition: attachment; filename=file.csv\' );
    header( \'Content-Transfer-Encoding: binary\' );
    header( \'Expires: 0\' );
    header( \'Cache-Control: must-revalidate\' );
    header( \'Pragma: public\' );
    header( \'Content-Length: \' . filesize($file) );

    readfile($file);

    exit;
}

add_action( \'handle_bulk_actions-edit-post\', \'wpse_287406_export_csv\', 10, 3 );

结束

相关推荐

当尝试向我的登录页面添加一个css文件时,会出现“Headers Always Sent”?

我看过的每一篇关于这个主题的教程都已经过时了,所以我希望这里的人能帮助我。我想为我的wp登录设置样式。带有CSS文件的php页面。但我想以一种在Wordpress更新时不会被覆盖的方式来做。据我所知,根据我读到的图茨,在我的主题函数中使用函数添加它的最佳方式。php文件。不幸的是,人们建议使用的代码并不适合我。这是我添加的代码。<?php function custom_login() { echo \'<link rel=\"stylesheet\" href=\"wplog