0

I'm making a Widget which presents users with categories as drop down list. When user selects category and press download, I want to get all posts under category and download them. I have faced a problem on downloading. I do something like

$xml = ''; //generated XML with Simple XML
header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="text.xml"');
echo $xml->asXML(); 

I get headers already sent. So I added buffering to init with code:

add_action('init', 'do_output_buffer');

function do_output_buffer() {
        ob_start();
}

and in my MY_Widget::widget() I add the code echo ob_get_clean(); after echo $xml->asXML(); explained above. Here it works except it exports whole page (with my XML) as single XML file.

Is there a way to download file without including whole page?

0

I just had to clean buffer and then do my stuffs and call exit

ob_clean(); //clear buffer
header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="text.xml"');
echo $xml->asXML(); 
exit();

Thanks to they guys here for solution!

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.