title: How can I use PHP headers() in WordPress to download files from my custom plugin page?
body: <p>I am trying to use <strong>PHP headers() in WordPress to download files from my plugin page view</strong>, but it is not working as this error message is returned for each header line present in my script:</p>
  
  <blockquote>
    <p>Warning: Cannot modify header information - headers already sent by (output started at D:\xampp\wordpress\wp-includes\formatting.php:5100) in D:\xampp\wordpress\wp-content\plugins\pxw-test\pxw-test.php on line 33</p>
  </blockquote>
  
  <p>And so on for each line of present headers(); in my script...</p>
  
  <p>You can have a look at the pxw-test.php file content (the plugin itself) below:</p>
  
  <pre><code>&lt;?php
      /*
          Plugin Name: Test Headers
          Description: Download File Test Plugin for Headers Response errors.
          Version:     1.0
          Author:      Tester
          Text Domain: pxw_test
      */
      // Exit if accessed directly
      if ( ! defined( 'ABSPATH' ) ) {
          exit;
      }
  
      //Admin Menu
      function pxw_test_menu() {
          // Create custom top-level menu
          add_menu_page(
          'Dashboard',
          'Test Headers',
          'manage_options',
          'pxw_test',
          'pxw_test_dashboard_page',
          null,
          20
          );
      }
  
  
      //Function needed to display top-level page as the main page
      function pxw_test_dashboard_page(){
          // Download
          if(isset($_POST['download'])){
  
              // File to download
              $filename = 'file.txt';
  
              // HTTP headers for downloads
              header("Pragma: public");
              header("Expires: 0");
              header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
              header("Content-Type: application/force-download");
              header("Content-Type: application/octet-stream");
              header("Content-Type: application/download");
              header("Content-Disposition: attachment; filename=$filename");
              header("Content-Transfer-Encoding: binary");
              header("Content-Length: ".filesize(plugin_dir_path(__FILE__).$filename));
  
              //You can try the following
              // while (ob_get_level()) {
              // ob_end_clean();
              // }
              // @readfile(plugin_dir_path(__FILE__).$filename);
          }
          else {
          }
  ?&gt;
  
          &lt;form action="" method="POST" enctype="multipart/form-data" style="text-align:center;padding: 100px 0;"&gt;
              &lt;button type="submit" onclick="this.form.submit();" style="background:red;padding:10px;color:#fff;cursor:pointer;"&gt;&lt;b&gt;Download File&lt;/b&gt;&lt;/button&gt;
              &lt;input type="hidden" name="download" /&gt;
          &lt;/form&gt;
  
  &lt;?php
      } # End of function pxw_test_dashboard_page().
  
      // Finally display the menu
      add_action('admin_menu', 'pxw_test_menu');
  ?&gt;
  </code></pre>
  
  <p>My goal is to <strong>download</strong> a file and <strong>delete</strong> the file after download.</p>
  
  <p><strong>Of course I am not looking for:</strong></p>
  
  <p>A <strong>function.php</strong> solution as I am building a plugin.</p>
  
  <p>But anything short and manageable from my plugin folder is welcomed.</p>
  
  <p>Such as:</p>
  
  <ol>
  <li><p>PHP</p></li>
  <li><p>JavaScript</p></li>
  <li><p>jQuery</p></li>
  <li><p>HTML5, such as <code>&lt;iframe&gt;</code>, <strong>but not</strong> the <code>&lt;a download&gt;&lt;/a&gt;</code> as I will need to make actions such as deleting the file after download and if the file  was downloaded or not.</p></li>
  </ol>