wordpress csv export includes html in the output

I am trying to dynamically create a downloadable csv file from within WordPress. The data to be included is correct, and the file is created for download successfully. However, the created csv file includes the template HTML output.

How can the data be output, without the HTML from the template?

Here is the function. The arrays are correctly populated with a sql query, but the results are identical to this.

function exportAsCSV (  ) {
  $csv = '';
  $header_array = array('Header 1', 'Header 2', 'Header 3');
  $data_array = array(
    array('Row 1 A', 'Row 1 B', 'Row 1 C'),
    array('Row 2 A', 'Row 2 B', 'Row 2 C'), 

  $csv .= implode(', ', $header_array);
  $csv .= '\n';

  foreach($data_array as $row){
    $csv .= implode(', ', $row);
    $csv .= '\n';  

    $now = gmdate('D, d M Y H:i:s') . ' GMT';

    header('Content-Type: text/csv');
    header('Expires: ' . $now);

    header('Content-Disposition: attachment; filename="data.csv"');
    header('Pragma: no-cache'); 

    echo $csv; 

The function is called as so:



I figured it out. More levels of output buffering. Now, I call ob_get_level() to determine how many times to ob_end_clean(). No more extra HTML in the output.

Need Your Help

How to get text td from check box clicked on grid?

jquery select text grid html-table

I need to get the text of a td on a grid, only the check box that was clicked and selected, not all of which are selected.

Getting the grasp of Couchbase using memcached buckets

php memcached couchbase

The case I have is that I currently have 3 memcached instances running at 3 different servers. My application is currently at the same 3 different nodes. All the applications servers know of all the

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.