How to export custom database data to excel file

Hi everyone I have the code below, but when I click on the link generated a new page is open with 0 display on it. What I'm doing wrong?

function list_reservation_to_csv() {

 global $wpdb;
 $table_name = $wpdb-prefix . 'prenotazione_eventi';
 $file = 'email_csv'; // ?? not defined in original code
 $results = $wpdb-get_results("SELECT * FROM $table_name",ARRAY_A);

 if (empty($results)) {
   return;
 }

 $csv_output = '"'.implode('";"',array_keys($results[0])).'";'."\n";;

 foreach ($results as $row) {
  $csv_output .= '"'.implode('";"',$row).'";'."\n";
 }
 $csv_output .= "\n";

 $filename = $file."_".date("Y-m-d_H-i",time());
 header("Content-type: application/vnd.ms-excel");
 header("Content-disposition: csv" . date("Y-m-d") . ".csv");
 header( "Content-disposition: filename=".$filename.".csv");
 print $csv_output;
 exit;

 }
add_action('wp_ajax_csv_pull','list_reservation_to_csv');

$ajax_url = admin_url('admin-ajax.php?action=csv_pull');

a href="?php echo $ajax_url; ?"buttonScarica csv/button/a

Topic ajax csv export Wordpress

Category Web


Try adding a nopriv hook

add_action('wp_ajax_nopriv_csv_pull','list_reservation_to_csv');

This adds the option to run the function without being logged in as a registered user.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.