Put all the code that handles the output of the CSV before your get_header()
call, then exit
:
<?php
/**
* CSV page template
*
*/
if($_SERVER['REQUEST_METHOD'] == "POST") {
$fileName = date("d-m-y") . '-bv-directory.csv';
$content = ""; // content added below
// Title of the CSV
$content = "Name,Address,Age,Phone \n";
// Data in the CSV
$content .= "\"John Doe\",\"New York, USA\",15,65465464 \n";
// Create csv and force download
header('Content-Type: application/csv');
header("Content-length: " . filesize($NewFile));
header('Content-Disposition: attachment; filename="' . $fileName . '"');
echo $content;
exit;
}
?>
<?php get_header(); ?>
<div id="sidebar-1">
<?php get_sidebar(); ?>
</div> <!-- End div#sidebar -->
<div id="primarycontent">
<?php get_template_part( '/loop', 'default' ); ?>
<h2>Export</h2>
<form action="" method="post">
<input name="submit" type="submit" value="Export">
<input type="hidden" name="submit" />
</form>
</div> <!-- End div#primarycontent -->
<?php get_footer(); ?>