【问题标题】:Warning: Cannot modify header information - headers already sent by - wp-includes\script-loader.php:2011) in警告:无法修改标头信息 - 标头已由 - wp-includes\\script-loader.php:2011) 发送
【发布时间】:2022-12-14 12:27:38
【问题描述】:

我已经创建了一个自定义函数,用于将表格数据导出到 WordPress 插件中的 Excel 工作表。

我的功能:-

   public function csv_download(){
        $header_row = array(
            0 => 'Sr.No',
            1 => 'name',
            2 => 'email',
            3 => 'Registration Date',
        );
        $data_rows = array();
        global $wpdb, $bp;
        $users = $wpdb->get_results( "SELECT ID, user_email, user_registered FROM {$wpdb->users} WHERE user_status = 0" );
        foreach ( $users as $u ) {
            $row = array();
            $row[0] =  $u->ID ;
            $row[1] = $u->user_name;
            $row[2] = $u->user_email;
            $row[3] = $u->user_registered;
            $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-type: text/csv' );
        header( "Content-Disposition: attachment; filename=\"report.csv\";" );
        header( 'Expires: 0' );
        header( 'Pragma: public' );
        header('Content-Type: application/force-download');
        fputcsv( $fh, $header_row );
        foreach ( $data_rows as $data_row ) {
            fputcsv( $fh, $data_row );
        }
        fclose( $fh );
        die();
        
    }

但我不断收到一条错误消息:

【问题讨论】:

    标签: wordpress custom-wordpress-pages


    【解决方案1】:

    您可能已经解决了这个问题,但也许我的回答会对其他人有所帮助。

    当尚未发送标头时,您应该将您的操作附加到一些早期挂钩,例如 admin_init 挂钩。

    所以:

    1. `add_action('admin_init', 'your_custom_function');
    2. your_custom_function 中,您可能应该验证 $_GET 变量以检查用户是否单击了您的链接或其他完全不同的内容
    3. 如果这是您的链接,您应该可以毫无问题地回显要下载的数据的标题和内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-09
      • 2011-01-15
      • 2012-03-31
      • 2012-02-28
      • 2011-05-09
      相关资源
      最近更新 更多