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 = “<html><body><table>”;
$message .= “<tr>”;
foreach( GFAPI::get_form( $form_id )[‘fields’] as $field ){
$message .= “<th>” . $field->label . “</th>”;
}
$message .= “</tr>”;
foreach ( $entries as $entry ) {
$message .= “<tr>”;
foreach( GFAPI::get_form( $form_id )[‘fields’] as $field ){
$value = rgar( $entry, $field->id );
$message .= “<td>” . $value . “</td>”;
}
$message .= “</tr>”;
}
$message .= “</table></body></html>”;
// **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 theinit
action, which runs when WordPress initializes.- Inside
schedule_weekly_gf_report()
, it checks if a weekly schedule forweekly_gf_report_hook
is already set. If not, it useswp_schedule_event()
to schedule theweekly_gf_report_hook
to run every week.
-
Email Sending:
send_weekly_gf_entries()
function is hooked to theweekly_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.
- Checks for errors during entry retrieval and logs them using
-
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.
- Uses
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.
Recent Comments