[wp-hackers] exporting a file in wp admin

Shinji Hiranaka shinji.hiranaka at pangaea.tw
Sun May 23 15:22:45 UTC 2010


Hi Steve,

How about code like below.

---start---

add_action( 'init', 'before_submit_header' );

function before_submit_header() {
 if( $_POST['submit'] === 'spreadsheet_download' ) {
   header( 'Content-Description: File Transfer' );
   header( 'Content-Type: application/vnd.ms-excel' );
   header( 'Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT' );
   header( 'Content-disposition: attachment; filename=download-logs.xls' );
   header( 'Pragma: no-cache' );
   echo $spreadsheet;
   die();
 }
}

---end---

Lastly,
you need to add html like below to somewhere in your admin page.
<input type="hidden" name="submit" value="spreadsheet_download">



Pangaea Taiwan Company Limited.
s.hiranaka

2010/5/23 Steve Taylor <steve at sltaylor.co.uk>:
> I'm trying to send a spreadsheet created with PHP on a WP admin page
> to the browser for download. The relevant code I've got is:
>
> header( "Content-Type: application/vnd.ms-excel" );
> header( 'Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT' );
> header( "Content-disposition: attachment; filename=download-logs.xls" );
> header( 'Pragma: no-cache' );
> echo $spreadsheet;
> exit;
>
> I gather there's a few different options for the header settings, but
> I think the problem I've got is more basic than that. What I'm seeing
> is just the normal WP admin page, with the spreadsheet data dumped as
> a string. These are the response headers (via Web Developer in
> Firefox):
>
> Date: Sun, 23 May 2010 12:28:56 GMT
> Server: Apache/2.2.3 (CentOS)
> X-Powered-By: PHP/5.1.6
> Expires: Wed, 11 Jan 1984 05:00:00 GMT
> Last-Modified: Sun, 23 May 2010 12:28:57 GMT
> Cache-Control: no-cache, must-revalidate, max-age=0
> Pragma: no-cache
> Keep-Alive: timeout=15, max=500
> Connection: Keep-Alive
> Transfer-Encoding: chunked
> Content-Type: text/html; charset=UTF-8
> 200 OK
>
> I suspect I'm missing the trick of clearing the header settings
> already specified by WP itself. I tried ob_clean() at the start, but
> then I just see a blank WP admin page.
>
> Any help appreciated!
>
> Steve Taylor
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list