title: Wordpress Export Custom Table to CSV
body: <p>I've got a custom database table that I'm storing information to and I need to export selected rows(id's) to a csv file. Currently I have added a an option for "Export" to my bulk actions and it will export the entire table to a CSV file, but I need to only export entries that have been checked for bulk option. Here is the code I have so far.</p>
  
  <p>The delete feature works when selecting multiple checkboxes.</p>
  
  <pre><code>function column_cb($item)
      {
          return sprintf(
              '&lt;input type="checkbox" name="id[]" value="%s" /&gt;',
              $item['id']
          );
      }    
  
  function get_bulk_actions()
          {
              $actions = array(
                  'delete' =&gt; 'Delete'
              );
              return $actions;
          }
  
  
      function process_bulk_action()
              {
                  global $wpdb;
                  $bp_table_name = $wpdb-&gt;prefix . 'custom_table'; // do not forget about tables prefix
  
          ?&gt;
                  &lt;script type="text/javascript"&gt;
  
                jQuery(document).ready(function() {
  
                  jQuery('&lt;option&gt;').val('export').text('&lt;?php _e('Export')?&gt;').appendTo("select[name='action']");
  
                  jQuery('&lt;option&gt;').val('export').text('&lt;?php _e('Export')?&gt;').appendTo("select[name='action2']");
  
                });
  
              &lt;/script&gt;
                  &lt;?php
                  if ('export' === $this-&gt;current_action()) {
                      ob_end_clean();
                      $sql = $wpdb-&gt;get_results( "SELECT * FROM $bp_table_name");
  
                      if (!$sql) {
                          die('Invalid query: ' . mysql_error());
                      }
  
                      // Get The Field Name
                      $output .= 'Exhibitor Company'. ',';
                      $output .= 'Exhibitor Representative'. ',';
                      $output .= 'Position Title'. ',';
                      $output .= 'Mailing Address'. ',';
                      $output .= 'City'. ',';
                      $output .= 'State'. ',';
                      $output .= 'Zip'. ',';
                      $output .= 'Website'. ',';
                      $output .= 'Office Phone'. ',';
                      $output .= 'Cell Phone'. ',';
                      $output .= 'Email'. ',';
                      $output .= 'Backup Email'. ',';
                      $output .= 'Products/Services'. ',';
                      $output .= 'Same Booth Space'. ',';
                      $output .= 'Booth Numbers'. ',';
                      $output .= 'Convention Center'. ',';
                      $output .= 'Expo Halls'. ',';
  
                      $output .="\n";
  
                      // Get Records from the table
  
                      foreach ($sql as $row) {
  
                      $output .="\n";
                      $output .='"'.$row-&gt;exhibitor_company.'",';
                      $output .='"'.$row-&gt;exhibitor_rep.'",';
                      $output .='"'.$row-&gt;title.'",';
                      $output .='"'.$row-&gt;address.'",';
                      $output .='"'.$row-&gt;city.'",';
                      $output .='"'.$row-&gt;state.'",';
                      $output .='"'.$row-&gt;zip.'",';
                      $output .='"'.$row-&gt;website.'",';
                      $output .='"'.$row-&gt;office_phone.'",';
                      $output .='"'.$row-&gt;cell.'",';
                      $output .='"'.$row-&gt;email.'",';
                      $output .='"'.$row-&gt;backup_email.'",';
                      $output .='"'.$row-&gt;products_services.'",';
                      $output .='"'.$row-&gt;same_space.'",';
                      $output .='"'.$row-&gt;booth_numbers.'",';
                      $output .='"'.$row-&gt;convention_center.'",';
                      $output .='"'.$row-&gt;expo_halls.'",';
  
                      }
                      $output .="\n";
                      // Download the file
  
                      $file = "custom_table";
                      $filename = $file."_".date("Y-m-d_H-i",time());
                      header("Content-type: application/vnd.ms-excel");
                      header("Content-disposition: csv" . date("Y-m-d") . ".csv");
                      header( "Content-disposition: filename=".$filename.".csv");
  
                      echo $output;
                      exit;
  
                  }
  
                  $entry_id = ( is_array( $_REQUEST['id'] ) ) ? $_REQUEST['id'] : array( $_REQUEST['id'] );
  
                  if ( 'delete' === $this-&gt;current_action() ) {
                      global $wpdb;
  
                      foreach ( $entry_id as $id ) {
                          $id = absint( $id );
                          $wpdb-&gt;query( "DELETE FROM $bp_table_name WHERE id = $id" );
                      }
                  }
              }
  </code></pre>