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 have created a download link for my plugin using add_submenu_page. When the link is clicked it gathers some files into a zip, then outputs the contents to the browser like so:

header('Content-Type: application/zip');
header('Content-Length: ' . filesize($file));
header('Content-Disposition: attachment; filename="file.zip"');
readfile($file);
unlink($file);

The issue is that wordpress by default wraps everything in it's admin theme. Is there a flag or something I can set to tell wordpress that for this particular page I don't want it to out the admin interface?

Edit: Here is the code I used to generate the submenu page:

  add_action('admin_menu', array($this, 'add_download_page'));

  function add_download_page() {
    add_submenu_page( 
        'options.php?post_type=spn_newsletter'
      , 'Download Newsletter' 
      , 'Download Newsletter'
      , 'manage_options'
      , 'spn_download'
      , array($this, 'do_download')
    );
  }
share|improve this question
    
Show the code you are using to create this submenu page. –  brasofilo Jan 17 '13 at 20:09
add comment

1 Answer

up vote 1 down vote accepted

I think you'll have to hook an earlier action and check if your subpage is being loaded, when the function to render the subpage happens it's too late to send headers. Your subpage's rendering function could just be blank because the earlier action would override it. I tried this with a subpage of themes.php and it seemed like it would work:

function wpa82041_do_download(){
    global $pagenow;
    if( 'themes.php' == $pagenow
    && 'spn_download' == $_GET['page'] ){
        // do your download stuff
    }
}

add_action( 'admin_init', 'wpa82041_do_download' );
share|improve this answer
    
Wow, very clever, that did exactly what I needed. Thanks. –  greatwitenorth Jan 17 '13 at 22:23
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.