Export directory results as CSV
-
I’m pretty sure this option doesn’t exist. I’m willing to write the code to do this, but I’m wondering if I can get some help and direction to build this out.
My foundational question: How would I programmatically retrieve the user profile fields that are defined? From here, I can probably build out the rest of the logic.
Thanks!
-
Hello @tedtang
Unfortunately, this functionality does not exist in our plugin at this time. Sorry, but customizations are not included in our support.
Regards.
If anyone is interested, this is what I used to get the form’s fields in sorted order:
$form_id = 494;$fields = get_post_meta( $form_id, '_um_custom_fields', true );
if ( ! empty( $fields ) && is_array( $fields ) ) {
foreach ( $fields as $field_key => $field ) {
if (isset($field["position"])) {
$field_array[$field["position"]] = $field_key;
}
}
ksort($field_array, SORT_NUMERIC);
}And the full approach…
In /ultimate-member/templates/members.php:
<div> <a id="download-as-csv" href="download.csv?<?php echo $query; ?>">Download as CSV</a> </div> <script> jQuery( document.body ).on( 'blur', '.um-directory .um-search-filter.um-text-filter-type input[type="text"]', function() { queryString = window.location.href.slice(window.location.href.indexOf('?') + 1); jQuery('#download-as-csv').attr('href', 'download.csv?' + queryString); }); </script>In my theme’s functions.php file:
<?php add_action('template_redirect','download_directory_as_csv'); function download_directory_as_csv() { if (is_user_logged_in() && str_starts_with($_SERVER['REQUEST_URI'], '/path-to-directory/download.csv?')) { include 'inc/generate_directory_csv.php'; exit(); } }In inc/generate_directory_csv.php:
<?php if (!is_user_logged_in()) { exit(); } $form_id = 494; global $wpdb; $field_prefix = 'filter_'; $field_suffix = '_ddb1c'; //Headers header("Content-type: application/x-msdownload",true,200); header("Content-Disposition: attachment; filename=download.csv"); header("Pragma: no-cache"); header("Expires: 0"); ob_start(); $df = fopen("php://output", 'w'); // Get the fields in a sorted array $fields = get_post_meta( $form_id, '_um_custom_fields', true ); if ( ! empty( $fields ) && is_array( $fields ) ) { foreach ( $fields as $field_key => $field ) { if (isset($field["position"])) { $field_array[$field["position"]] = $field_key; $field_names[$field_key] = $field["label"]; } } ksort($field_array, SORT_NUMERIC); } // Build the header $header = array(); foreach ( $field_array as $key => $field ) { $header[] = $field_names[$field]; } fputcsv($df, $header); // Reproduce the search query and select the right user IDs $joins = array(); $where_clauses = array(); foreach ($_GET as $key => $value) { if (str_starts_with($key, $field_prefix) && str_ends_with($key, $field_suffix)) { $field = $key; $field = str_replace($field_prefix, '', $field); $field = str_replace($field_suffix, '', $field); $joins[] = "LEFT JOIN {$wpdb->usermeta} {$field} ON {$field}.user_id = u.ID"; $where_clauses[] = $wpdb->prepare( "{$field}.meta_key = %s and {$field}.meta_value LIKE '%%%s%%'", $field, $value ); } } $joins[] = "LEFT JOIN {$wpdb->usermeta} role ON role.user_id = u.ID"; $where_clauses[] = $wpdb->prepare( "role.meta_key = %s and role.meta_value LIKE '%%%s%%'", ($wpdb->get_blog_prefix( $blog_id ) . "capabilities"), "directory_member_role" ); $sql_join = implode( ' ', $joins ); $sql_where = implode( ' AND ', $where_clauses ); $sql_where = ! empty( $sql_where ) ? 'AND ' . $sql_where : ''; $wpdb->query( 'SET SQL_BIG_SELECTS=1' ); $user_ids = $wpdb->get_results( "SELECT DISTINCT u.ID FROM {$wpdb->users} AS u {$sql_join} WHERE 1=1 {$sql_where}" ); // Get the data for each user foreach ($user_ids as $user_id) { $id = $user_id->ID; $wpdb->query( 'SET SQL_BIG_SELECTS=1' ); $user_data = $wpdb->get_results( "SELECT meta_key,meta_value FROM {$wpdb->usermeta} AS um WHERE {$wpdb->prepare( "um.user_id = %s", $id )}" ); //Translate to an array for easy access $user_array = array(); foreach ($user_data as $row_data) { $user_array[$row_data->meta_key] = $row_data->meta_value; } $row = array(); foreach ( $field_array as $key => $field ) { $data = maybe_unserialize($user_array[$field]); if (is_array($data)) { $data = implode(',', $data); } $row[] = $data; } fputcsv($df, $row); } fclose($df); echo ob_get_clean();Modified the JavaScript to work with removing filters:
<script> jQuery(document).ready( function() { jQuery( document.body ).on( 'blur', '.um-directory .um-search-filter.um-text-filter-type input[type="text"]', function() { queryString = ''; if (window.location.href.includes('?')) { queryString = window.location.href.slice(window.location.href.indexOf('?') + 1); } jQuery('#download-as-csv').attr('href', 'download.csv?' + queryString); }); wp.hooks.addFilter( 'um_member_directory_ignore_after_search', 'namespace', function( ){ queryString = ''; if (window.location.href.includes('?')) { queryString = window.location.href.slice(window.location.href.indexOf('?') + 1); } jQuery('#download-as-csv').attr('href', 'download.csv?' + queryString); } ); }); </script>
The topic ‘Export directory results as CSV’ is closed to new replies.