title: How to download CSV file from function in WordPress plugin?
body: <p>I've built a plugin for a client so that they can download data as a CSV file. It's been set up so that when the user clicks on a link in the menu, the CSV should just automatically download. However, it doesn't quite work like that, and just loads the function as a page in the WordPress backend.</p>
  
  <p>This is the code I have for the function:</p>
  
  <pre><code>function download_payment_csv() {
      include 'lib/connection.php';
  
      $csv_output = '';
  
      $values = $db-&gt;query('SELECT * FROM tbPayments ORDER BY date DESC');
  
      $i=0;
  
      while ($rowr = mysql_fetch_row($values)) {
          for ($j=0;$j&lt;$i;$j++) {
              $csv_output .= $rowr[$j].",";
          }
          $csv_output .= "\n";
      }
  
      header("Pragma: public");
      header("Expires: 0");
      header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
      header("Cache-Control: private", false);
      header("Content-Type: application/octet-stream");
      header("Content-Disposition: attachment; filename=\"report.csv\";" );
      header("Content-Transfer-Encoding: binary");
  
      echo $csv_output;
  
  }
  </code></pre>
  
  <p>And as I said, it just returns a blank screen. Any help would be appreciated!</p>
  
  <p><strong>EDIT</strong>
  So this is the code I'm now working with, taking bits from what's been said already.</p>
  
  <pre><code>function download_payment_csv() {
  
      include 'lib/connection.php';
  
      $csv_output = '';
  
      $values = load_payment_csv();
  
      $fp = fopen("php://output", "w");
  
      $file = 'test_export';
      $filename = $file."_".date("Y-m-d_H-i",time());
      header("Content-Type: text/csv");
      header("Content-Disposition: attachment; filename=".$filename.".csv");
      // Disable caching
      header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1
      header("Pragma: no-cache"); // HTTP 1.0
      header("Expires: 0"); // Proxies
      header("Content-Transfer-Encoding: UTF-8");
  
      if(count($values) &gt; 0) {
          foreach($values as $result) {
              fputcsv($fp, $result);
          }
      }
  
      fclose($fp);
  
  }
  </code></pre>
  
  <p>This generates a CSV, but there is a problem with it.The problem is that when viewing the page it doesn't download it as a CSV, it just outputs the contents of the CSV in to the page. However, adding this function to the top of the plugin:</p>
  
  <pre><code>add_action('admin_init','download_payment_csv');
  </code></pre>
  
  <p>This then triggers a download when the menu link is clicked, which is fine. But it triggers it for every menu item in the plugin, which is wrong. It should only trigger when the download link is clicked.</p>