DEV Community

Cover image for How to Schedule a Weekly Roundup of Gravity Forms Entries on Your Website
Rana Umara Azeem
Rana Umara Azeem

Posted on

How to Schedule a Weekly Roundup of Gravity Forms Entries on Your Website

In this blog post, we will show you how to schedule a weekly roundup of your Gravity Forms entries and send them to your email inbox directly from your website. This way, you can stay up-to-date on new form submissions without having to manually check your forms every day.

What you will need:

A WordPress website with Gravity Forms installed and activated
A basic understanding of PHP
Steps to Schedule Weekly Roundup of Gravity Forms Entries

Add the code snippet to your theme’s functions.php file or a custom plugin
The first step is to add the following code snippet to your theme’s functions.php file or a custom plugin. This code snippet will use WordPress’s built-in scheduling system to automatically generate and send a report of your Gravity Forms entries once a week.

// Schedule the weekly report function on WordPress initialization
add_action( ‘init’, ‘schedule_weekly_gf_report’ );

function schedule_weekly_gf_report() {
if ( ! wp_next_scheduled( ‘weekly_gf_report_hook’ ) ) {
wp_schedule_event( time(), ‘weekly’, ‘weekly_gf_report_hook’ );
}
}

// Function to send the weekly report email
add_action( ‘weekly_gf_report_hook’, ‘send_weekly_gf_entries’ );

function send_weekly_gf_entries() {

// 1. Define Form ID and Recipient Email
$form_id = 1; // Replace with your form ID
$recipient_email = ‘client@example.com’; // Replace with your recipient email
$subject = ‘Weekly Gravity Forms Entries’;

// 2. Calculate Date Range (Last 7 Days)
$end_date = date( ‘Y-m-d H:i:s’ );
$start_date = date( ‘Y-m-d H:i:s’, strtotime( ‘-7 days’ ) );

// 3. Query Entries
$search_criteria = array(
‘date_from’ => $start_date,
‘date_to’ => $end_date,
);
$sorting = array();
$paging = array( ‘page_size’ => 200 ); // Adjust page size if needed

$entries = GFAPI::get_entries( $form_id, $search_criteria, $sorting, $paging );

if ( is_wp_error( $entries ) ) {
error_log( ‘Error retrieving entries: ‘ . $entries->get_error_message() );
return;
}

if ( empty( $entries ) ) {
wp_mail( $recipient_email, $subject, ‘No entries this week.’ );
return;
}

// 4. Format Email Body (Example: Table)
$message = “

”;
$message .= “”;
foreach( GFAPI::get_form( $form_id )[‘fields’] as $field ){
$message .= “”;
}
$message .= “”;

foreach ( $entries as $entry ) {
$message .= “

”;

foreach( GFAPI::get_form( $form_id )[‘fields’] as $field ){

$value = rgar( $entry, $field->id );

$message .= “”;

}

$message .= “”;

}

$message .= “

” . $field->label . “
” . $value . “
”;

// 5. Send Email
$headers = array(‘Content-Type: text/html; charset=UTF-8’);
$mail_sent = wp_mail( $recipient_email, $subject, $message, $headers );

if ( ! $mail_sent ) {
error_log( ‘Error sending email.’ );
}
}

Functionality

Scheduling:

schedule_weekly_gf_report() function is hooked to the init action, which runs when WordPress initializes.
Inside schedule_weekly_gf_report(), it checks if a weekly schedule for weekly_gf_report_hook is already set. If not, it uses wp_schedule_event() to schedule the weekly_gf_report_hook to run every week.
Email Sending:

send_weekly_gf_entries() function is hooked to the weekly_gf_report_hook. This is where the actual email generation and sending logic resides.
Form ID and Recipient:

$form_id and $recipient_email variables are defined at the beginning. Replace these with your actual form ID and the recipient’s email address.
Date Range:

$start_date and $end_date are calculated to represent the last 7 days.
Entry Query:

GFAPI::get_entries() is used to fetch entries from the specified form within the date range.
'status' => 'active' in the $search_criteria is crucial to exclude spam submissions.
Error Handling:

Checks for errors during entry retrieval and logs them using error_log().
If no entries are found, sends a “No entries this week” email.
Email Formatting:

Creates an HTML table to display the entries.
Iterates through form fields to generate table headers.
Iterates through entries to populate table rows with field values.
Email Sending:

Uses wp_mail() to send the formatted email.
Includes basic error handling for email sending.
Key Points and Considerations

Security: If you’re dealing with sensitive data, consider encrypting the email or using a more secure method of data transfer.
Large Datasets: For forms with a large number of entries, you might need to optimize the query or use a different approach, such as exporting to a CSV file.
Customizations: You can customize this code to:
Include additional data in the email (e.g., submission timestamps, user IP addresses).
Format the email differently (e.g., using a different template).
Send the report to multiple recipients.
Add more advanced filtering options (e.g., by entry status, specific fields).
Testing: Test the code thoroughly before deploying it to your production site.
How to Use:

Install Gravity Forms: Make sure you have Gravity Forms installed and activated on your WordPress site.
Add the Code: Place the code snippet in your theme’s functions.php file or create a custom plugin.
Replace Placeholders: Update $form_id and $recipient_email with your actual values.
Test: Trigger the send_weekly_gf_entries() function manually once to test the email delivery and formatting.
This code provides a solid foundation for scheduling weekly Gravity Forms entry reports. You can customize it further to meet your specific needs and reporting requirements.

If you have any further questions or need additional assistance, feel free to ask! Contact us Call +15309688916 or email info@creativetugs.com

Top comments (0)