Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am currently writing a plugin that contains a report based on a mysql query. I would like to export this query information to CSV. Simple enough, I've done it many times outside of WordPress.

I'm currently receiving an error regarding headers already sent. I know what causes this error, but cannot find in my code where the issue is.

I have tried removing all other plugins, and even tried other plugins that also export CSV. The plugins that export CSV work just fine, and when my plugin is the only one activated, the error is still thrown so I am pretty sure it's an error on my part.

Also need to include that this is on a multisite network activated plugin.

The error points to the first header line:

    if (isset($_POST['txtExport'])) {
  header("Content-type: text/x-csv");
  header("Content-Disposition: attachment; filename=birts.csv");
  header("Pragma: no-cache");
  header("Expires: 0");

  foreach ($incidentReport as $vals) {
     echo "\"".$vals->student_id."\",\"".$vals->incident_desc."\",\"".$vals->incident_time."\",\"".$vals->incident_rating."\",\"".$vals->reported_by."\"\n<br/>";
  }
  exit;
    }

Below is the actual triggered error: Warning: Cannot modify header information - headers already sent by (output started at /home/plumwd/public_html/multisite/wp-admin/admin-header.php:28) in /home/plumwd/public_html/multisite/wp-content/plugins/bart/birts_dashboard.php on line 20

I have WP_DEBUG set to true and am showing no other errors.

Thanks!

share|improve this question
add comment

2 Answers 2

I figured out what the problem was. In the end, I added &noheader=true to my form action. This allowed me to submit my own headers without calling the WP admin headers.

share|improve this answer
add comment

I've seen this now and again back in my days of developing in PHP. Usually when something comes up like this, it means that something in your script or some include of your script has already leaked a few characters out to be written and so the headers for the page has already been sent. I've seen that usually in this case that it could be something as simple as a space character...maybe even a carriage return (?)...that exists before the in one of these files. Sadly, if it's not in the main script itself, this usually means going through your dependent include files and hunting it down.

share|improve this answer
    
Sadly that's not it. I really think it's because it's wrapped in the WP framework and the admin header is sending the information before my header calls are run. I have taken everything out of the plugin minus my dashboard page with the report and it's still throwing the error. If I add ob_start() to admin-header.php the csv gets generated but all the html prior to my csv data is also added to it. –  plumwd Oct 8 '11 at 18:47
    
Wanted to add, that I do believe what you have said is true, but I really think the problem is within birts_dashboard.php. I can't find the issue in there. –  plumwd Oct 8 '11 at 19:06
    
Ah. I'm afraid I'm out of my element at this point, having never done WP related development. Is there anything in the code of those other CSV exporting plugins that could provide a clue? It sounds like to me they would all have to overcome the admin header issue if the framework is indeed pushing them out. –  Hung Nguyen Oct 8 '11 at 22:04
    
I've looked at the code and I'm basically doing what they are, so not super sure what's going on. I will figure it out! –  plumwd Oct 9 '11 at 2:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.