Skip to content

Instantly share code, notes, and snippets.

@rickalday
Created June 29, 2026 19:54
Show Gist options
  • Select an option

  • Save rickalday/cb614b9f73e9771d97ec31e22f62c0be to your computer and use it in GitHub Desktop.

Select an option

Save rickalday/cb614b9f73e9771d97ec31e22f62c0be to your computer and use it in GitHub Desktop.
Export WP Users with custom fields
<?php
function free_custom_export_users() {
if ( isset( $_GET['export_my_users'] ) && current_user_can( 'manage_options' ) ) {
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=users_export.csv');
$output = fopen('php://output', 'w');
// Define your column headers here (Add your custom field names)
fputcsv($output, array('ID', 'Username', 'Email', 'My Custom Field 1', 'My Custom Field 2'));
$users = get_users(array('fields' => 'all'));
foreach ($users as $user) {
// Fetch your custom field data using the exact meta keys
$custom_field_1 = get_user_meta($user->ID, 'your_custom_meta_key_1', true);
$custom_field_2 = get_user_meta($user->ID, 'your_custom_meta_key_2', true);
fputcsv($output, array(
$user->ID,
$user->user_login,
$user->user_email,
$custom_field_1,
$custom_field_2
));
}
fclose($output);
exit;
}
}
add_action('admin_init', 'free_custom_export_users');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment