Admin plugin, how can I output a different content-type?

I’m creating a plugin with an admin page that needs to:

  • stream a file for download (set content-type & other headers)
  • display HTML without the admin menu etc

In both of these cases the ‘pages’ must be accessible only to logged in administrators. So far I’ve found one way to accomplish both of these, by including wp-blog-header.php in a php file, checking the user is an admin and doing things myself from then on, as below.

Read More
require('../../../wp-blog-header.php');

if (!current_user_can('administrator'))
{
    wp_die( __('You do not have sufficient permissions to access this page.') );
} 

...set headers & stream file

Is there a better way of doing this?

Is there any reason I shouldn’t do it this way?

Thanks 🙂

Related posts

Leave a Reply

2 comments

  1. I’ve done this two ways:

    1) – a csv export function – detect that special content type handling is required BEFORE wp outputs anything.

    add_action ('plugins_loaded',           'amr_meta_handle_csv');
    
    function amr_meta_handle_csv ($csv, $suffix='csv') {
    // chcek if there is a csv request on this page BEFORE we do anything else ?
    if (( isset ($_POST['csv']) )) {
    // do some stuff
          to_csv ($csv, $suffix)
    }
    }
    
    
    function to_csv ($csv, $suffix) {
    /* create a csv file for download */
        if (!isset($suffix)) $suffix = 'csv';
        $file = 'userlist-'.date('YmdHis').'.'.$suffix;
        header("Content-Description: File Transfer");
        header("Content-type: application/octet-stream");
        header("Content-Disposition: attachment; filename=$file");
        header("Pragma: no-cache");
        header("Expires: 0");
        echo $csv;
        exit(0);   /* Terminate the current script sucessfully */
    }       
    

    Another way was more feed oriented, but same principle, except wp does the special handling detection (checks for ?feed=ics or ). Put the add_feed code in an init action.

    add_feed('ics', 'ical_feed');
    

    function ‘ical_feed’ then does the whole header etc.