WordPress   

Get gravity form leads from DB and send them to admin as a CSV file in WordPress

In this post, we will talk about getting all the Gravity form submissions for a specific form and sending those to the admin email as a CSV file. By using this method we can select the leads according to the specific time slots. In this example, we will select leads from 8:00 PM of the previous day up to 7:59 PM of the current day.

Generate two date strings 

First, we will use two small functions to create the required time slot to select the gravity form leads from the database. These can be modified to return the different time slots according to the project requirements.

function get_start_time(){
    $start = date('Y-m-d 20:00:00',strtotime("-1 days"));
    return $start;
}
function get_end_time(){
    $end = date('Y-m-d 20:00:00');
    return $end; 
}

Getting gravity from leads from the database

Our next step will be getting the leads from the database and saving those into a CSV file. For this, we will use the following function which gets the data from the DB tables using WPDB.  You can hook this function into WP cron to execute it at a specific time or you can use custom triggers or actions.

Note – form_id is a required parameter for this function.

function get_gravity_form_leads($form_id){
    global $wpdb;
    $gf_entries = $wpdb->prefix . 'gf_entry';
    $gf_entries_meta = $wpdb->prefix . 'gf_entry_meta';
    $startRange = get_start_time();
    $endRange = get_end_time();
    $results = $wpdb->get_results( "SELECT id FROM $gf_entries WHERE form_id = $form_id AND status = 'active' AND date_created >= '$startRange' AND date_created < '$endRange'"); 
    if(!empty($results)){    
        $labelCount = 1;
        $leadCount = 1;
        $leads_data = array();
        foreach($results as $row){ 
            $gf_entries_rows = $wpdb->get_results( "SELECT * FROM $gf_entries_meta WHERE form_id = $form_id AND entry_id = {$row->id}");
            $totalFields = $gf_entries_rows->num_rows;
            $fieldCount = 1;
            foreach($gf_entries_rows as $gf_entry){ 
                if($leadCount == 1){
                    $fieldData = GFAPI::get_field( $form_id, $gf_entry->meta_key );
                    if($fieldCount == 1){
                        $leads_data[0][0] = 'GF Entry ID';
                        $leads_data[0][$fieldCount] = $fieldData['label'];
                    }
                    else{
                        $leads_data[0][$fieldCount] = $fieldData['label'];
                    }
                }
                if($fieldCount == 1){
                    $leads_data[$leadCount][0] = $gf_entry->entry_id;
                    $leads_data[$leadCount][$fieldCount] = $gf_entry->meta_value;
                }
                else{
                    $leads_data[$leadCount][$fieldCount] = $gf_entry->meta_value;
                }
                $fieldCount = $fieldCount + 1;
            }
            $leadCount = $leadCount + 1;
            $labelCount = $labelCount + 1;
        }
        if(!empty($leads_data)){
            $file = fopen(WP_CONTENT_DIR."/leads/leads.csv","w");
            foreach ($leads_data as $line) {
              fputcsv($file, $line);
            }
            fclose($file);
            $attachments = array(WP_CONTENT_DIR."/leads/leads.csv");
            send_leads_admin($attachments);
        }
    }  
}

Sending Leads to admin

Last but not least, send the CSV file to the admin. It can be accomplished by the following function.

function send_leads_admin($attachments){
    $to = get_option('admin_email'); 
    $subject     = "Contact form entries for today";
    $message     = "Please check the attached file.";
    $headers     = 'From: [email protected]' . "\r\n";
    $mailsent = wp_mail( $to, $subject, $message, $headers, $attachments);
}
Need Professional Support for Your Website Problems?

Whether you're facing website issues or struggling with code implementation, our team is here to assist. Hire us to get your website back on track.

Hire Us