If you are selling tickets to events using Events Calendar with Events Tickets add on and your registration form includes custom fields, you might have noticed that the default export feature does not include all the custom information from the registration form.

The code snippet below will create a new WordPress Admin page for you where you can export an xls document that includes all the custom registration fields. Be sure to replace the $custom_fields list with the fields from your registration form.
//Attendee export.
// Add a new menu item and page in the admin dashboard
function register_export_attendees_menu_page() {
add_menu_page(
'Export Attendees', // Page title
'Export Attendees', // Menu title
'manage_options', // Capability
'export-attendees', // Menu slug
'export_attendees_page', // Function to display the page content
'dashicons-download', // Icon URL
6 // Position
);
}
add_action('admin_menu', 'register_export_attendees_menu_page');
// Display the content of the new admin page
function export_attendees_page() {
?>
<div class="wrap">
<h1>Export Event Attendees</h1>
<form method="get" action="<?php echo admin_url('admin-post.php'); ?>">
<input type="hidden" name="action" value="export_attendees">
<label for="event_id">Select Event:</label>
<select name="event_id" id="event_id" required>
<?php
$events = tribe_get_events(array(
'posts_per_page' => -1,
));
foreach ($events as $event) {
echo '<option value="' . $event->ID . '">' . $event->post_title . '</option>';
}
?>
</select>
<input type="submit" value="Export" class="button button-primary">
</form>
</div>
<?php
}
// Export attendees function
function export_event_attendees($event_id) {
if (!class_exists('Tribe__Tickets__Tickets')) {
return;
}
$attendees = Tribe__Tickets__Tickets::get_event_attendees($event_id);
if (empty($attendees)) {
return;
}
$csv_output = fopen('php://output', 'w');
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=attendees.csv');
// Add CSV headers
$headers = array('Attendee Name', 'Email', 'Ticket Type', 'Order ID', 'Purchase Date');
// Add custom fields headers
$custom_fields = array('dietary-restrictions', 'referrals'); // Replace with your custom fields
$headers = array_merge($headers, $custom_fields);
fputcsv($csv_output, $headers);
foreach ($attendees as $attendee) {
$row = array(
$attendee['holder_name'],
$attendee['holder_email'],
$attendee['ticket_name'],
$attendee['order_id'],
$attendee['post_date']
);
foreach ($custom_fields as $field) {
$row[] = $attendee['attendee_meta'][$field] ?? '';
}
fputcsv($csv_output, $row);
}
fclose($csv_output);
exit;
}
// Handle the export action
add_action('admin_post_export_attendees', function() {
if (isset($_GET['event_id'])) {
export_event_attendees($_GET['event_id']);
}
});
Wishing your event to be a great success!