5

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.

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 :)

2

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.

-1

Take a look at the WordPress HTTP API. http://core.trac.wordpress.org/browser/tags/3.3.2/wp-includes/http.php

As far as the rest of plugin development. Read the plugin API or about writing a plugin.

  • 1
    The HTTP API is for fetching external resources, not for sending pages. And the asker obviously knows already the plugin basics. – fuxia May 22 '12 at 4:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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