0

I am writing a plugin which requires exporting custom taxonomies in csv on submit button click. I am using get_terms() to export taxonomies terms. I have written a class file which handle this operation and this file is included on the top of plugin main file.

I have searched about it and found out that get_terms() doesn't work until theme function.php is not loaded. So I want to know where should I include the class file. I have checked various hooks also but problem is that if I include it on admin_menu or admin_init hooks then it also outputs some other text of wordpress in exported csv file.

So please let me know how should I include the class file.

1 Answer 1

1

If you use admin_init no text should be output in the file unless you have some php notice.

Edit:

The following code should do.

<?php
/**
 * download_custom_taxonomy_csv_154304
 */
add_action('admin_init', 'download_custom_taxonomy_csv_154304');
function download_custom_taxonomy_csv_154304 () {

    // Check for GET request
    if ( isset( $_GET['download_ct_csv'] ) ){
        header('Content-type: text/csv');
        header('Content-disposition: attachment;filename=myct.csv');

        // Run here your code

        exit;
    }

}
4
  • Could you be a bit more verbose? How is this related to the question and how does it solve the problem?
    – kaiser
    Commented Jul 11, 2014 at 15:59
  • 1
    It's related as the question come from the wrong assertion that admin_init will not do the job while it's actually a good chooice. However i'll add a bit of code to make it clearer. Commented Jul 11, 2014 at 16:06
  • I have already tested with admin_init and init but both are outputting content. Finally, I found solution by using ajax.
    – Pritesh
    Commented Jul 12, 2014 at 13:04
  • It't not supposed to output content though. If you have an example i could check it out. Commented Sep 15, 2014 at 11:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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