Skip to content

Instantly share code, notes, and snippets.

@lorenzocaum
Created September 14, 2020 22:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lorenzocaum/8ed98fc063184c6b88361b6f43c910aa to your computer and use it in GitHub Desktop.
Save lorenzocaum/8ed98fc063184c6b88361b6f43c910aa to your computer and use it in GitHub Desktop.
//Code snippet to add wp_usermeta data meta data to csv export:

//----------------------------------------------------------------------------------------------------------//
// Used by: Registraction CSV Export (button Filtered CSV Export)                                           //
// Purpose: Include additional fields from the Ultimate Member Plugin data from the wp_usermeta table       // 
//----------------------------------------------------------------------------------------------------------//
add_filter( 'FHEE__EE_Export__report_registrations__reg_csv_array', 'ssci_add_um_fields_to_export', 10, 2);

function ssci_add_um_fields_to_export( $reg_csv_array, $reg_row ) {

   // use global wpdb object to access db (select user meta fields to export)
   global $wpdb;

   $meta_keys_include = array(
			'member_program_yn',
			'dvsv_organization',
			'phone_number',
			'kcsdv_address',
			'city_kcsdv',
			'state_kcsdv',
			'title',
			'your_profession',
			'job_type',
			'professional_type'
	        );

    // Initialize User ID Column to Nothing
    $reg_csv_array['User ID'] = ''; 
    // Initialize All meta key Values to Nothing 
    foreach ($meta_keys_include as $column) {       
    	$reg_csv_array[$column] = '';     
    }
    
    //Do we have a wp_user for this attendee record?
	$user_id = EE_WPUsers::get_attendee_user($reg_row['Registration.ATT_ID']);
        
	if (!empty($user_id)) {
    	//You have a user id saved to $user_id.
    	$reg_csv_array['User ID'] = $user_id;  

//-------START Block
        
        // Now, let get a the meta value for this user id and only include values for ones in the meta_keys_include array
	// Define Empty Arrays
        $metakeys = array();
        $metavalues = array();

        $query1 = "SELECT meta_key FROM $wpdb->usermeta WHERE user_id =%d";
        $result1 = $wpdb->get_var($wpdb->prepare($query1, (int)$user_id)); 
        if ( mysqli_num_rows($result1) > 0 ) {
        	while ( $row = mysqli_fetch_assoc($result1) ) {
                $metakeys[] = $row;
            }    
        }    
        
        $query2 = "SELECT meta_value FROM $wpdb->usermeta WHERE user_id =%d";
        $result2 = $wpdb->get_var($wpdb->prepare($query2, (int)$user_id));  
        if ( mysqli_num_rows($result2) > 0 ) {
        	while ( $row = mysqli_fetch_assoc($result2) ) {
                $metavalues[] = $row;
            }    
        }    
        
        
        $idx = 0;
        foreach ($metakeys[0] as $metakey) {
	    	if ( in_array( $metakey, $meta_keys_include ) ){
		    	$reg_csv_array[$metakey] = $metavalues[$idx];
            }
            $idx++;
        }    

//-------STOP Block

        
//      $results = $wpdb->get_results( 
//                   $wpdb->prepare("SELECT meta_key, meta_value FROM {$wpdb->wp}usermeta WHERE user_id=%d", $user_id); 
        
//	// Loop thru all meta_keys and only include values for matched include keys
//	foreach ($results as $key => $value) {
//	    if ( in_array( $key, $meta_keys_include ) ){
//       	$reg_csv_array[$key] = $value;             
//	    }	
                        
	}
    
   // always return csv array		
   return $reg_csv_array;

} //End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment