ubscribe', // The unique ID - that is used to identify this menu item 'export_subscribe_page', // The function that displays the content of the page 'dashicons-download', // The icon for this menu item 80 // The position of this menu item in the menu ); } add_action( 'admin_menu', 'add_export_subscribe_menu' ); // Define the function that displays the content of the page function export_subscribe_page() { global $wpdb; // Retrieve the data from the database $table_name = $wpdb->prefix . 'emc'; $results = $wpdb->get_results( "SELECT * FROM $table_name" ); // Create a new CSV file $filename = 'my_export.csv'; // Clear any previous output that may have been generated ob_clean(); // Use output buffering to capture the CSV data ob_start(); // Write the column headers to the CSV file $headers = array( 'id', 'time', 'emailid' ); fputcsv( fopen( "php://output", "w" ), $headers ); // Loop through the query results and write each row to the CSV file foreach ( $results as $row ) { $data = array( $row->id, $row->time, $row->emailid ); fputcsv( fopen( "php://output", "w" ), $data ); } // Get the captured CSV data $csv = ob_get_clean(); // Prompt the user to download the file header( 'Content-Type: text/csv' ); header( 'Content-Disposition: attachment; filename="' . $filename . '"' ); echo $csv; exit; } ?>

About us