• Ok, i’m building a plugin that posts some stuff to a table. The admins can see the answers in a table, and i’m trying to make a “download data to XML”-function.

    The plugin creates a menu option and the table with answers is displayed in the wpwrap div. There fore, i can’t do this the traditional way with

    header('Content-type: text/plain');
    header('Content-disposition: attachment; filename="MyXML.xml"');
    /* echo the content as XML */

    since headers has been sent. I have a function that generates a string with correct formatted XML, how can i now send this string as a file to the admins browser for download?
    I would prefer not to store the file physically on the server for downloading.

    In short, it works like this;
    The “Generate XML”-button is in a small form, it posts this command (‘do’) along with the id number of the table i want to get data from. In the top of this file, i check for $_POST[‘do’] and checks if its set to ‘XML’. In chat case it generates the string, and the problem i have from there is to send it as a downloadable file to the browser.

    Any suggestions?

Viewing 1 replies (of 1 total)
  • Mabye there is a more elegant way. But I would register a ghost page (a subpage with the parent slug set to ‘null’) to do the XML generation and downloading.
    This way you can still use a callback function to handle that page’s URL (so to speak) and yet the page won’t appear in the menu.

    add_submenu_page( null, '', '', $capability, 'customGhostPageSlug', 'generateXMLFile' );

    Then your download-XML-button could either be a simple link to that ghost page (href="admin.php?page=customGhostPageSlug") or if you could target a form to that page

    <form action="admin.php?customGhostPageSlug"...

    On that ghost page (respectively in the callback function for it) you can do the XML generation and also setting the header to force a download, etc.

Viewing 1 replies (of 1 total)
  • The topic ‘Building a plugin, can't send header()’ is closed to new replies.