title: How to download exported csv file in Wordpress
body: <p>I work on the plugin that creates menu in wp-admin/ side and shows the table with some data. I need to generate CSV ( it's going correct ) and give user to donwload it automatically.  I know, that I have to add the headers like these </p>
  
  <pre><code>header('Content-type: application/csv');
  header('Content-Disposition: attachment; filename="' . $csv_file_name . '"');
  </code></pre>
  
  <p>But unfortunately it does not work for wordpress admin-side.
  Again - the generating of CSV is going correct, but it displays just a text of csv file below the table, but don't give the file</p>
  
  <p>Here is full code</p>
  
  <pre><code>if ( isset( $_REQUEST['export_csv'] ) ) {
          global $wpdb;
          $csv_source_array = $wpdb-&gt;get_results ( $wpdb-&gt;prepare( " SELECT name, email, time, text FROM {$table_name} " ), ARRAY_N );
          $csv_file_name = 'nba.rally.'.date(Ymd).'.csv';
          $csv_header_array = array( "Name", "Email", "Date", "Message" );        
  
              if (isset($csv_source_array)) {
  
                  header('Content-type: application/csv');
                  header('Content-Disposition: attachment; filename="' . $csv_file_name . '"');
  
              ob_start();
                  $f = fopen('php://output', 'w') or show_error("Can't open php://output");
                  $n = 0;
  
  
                      if (isset($csv_header_array)) {
                          if ( !fputcsv($f, $csv_header_array, ';'))
                          {
                              echo "Can't write line $n: $line";
                          }
                      }
  
                  foreach ($csv_source_array as $line)
                  {
                      $n++;
  
                      if ( !fputcsv($f, $line, ';'))
                      {
                          echo "Can't write line $n: $line";
                      }
                  }
                  fclose($f) or show_error("Can't close php://output");
                  $csvStr = ob_get_contents();
                  ob_end_clean();
  
                  echo $csvStr;
              }
  
  
          }   
  </code></pre>
  
  <p>Thanks for an advance for any answers. </p>