Is there any way to add a column on the Forms list page (…/wp-admin/admin.php?page=gf_edit_forms) that shows the date of each form’s latest entry?
Hi, John.
To add a column on the Forms List page that shows the date the form received the last entry, you’d need:
- Add a custom column to the gform_form_list_columns filter.
- Create a separate function to populate the custom column with the gform_form_list_column_{COLUMN+NAME} dynamic action.
- On that function, retrieve the “submitted on” date of the last entry from the form’s entries object.
- Within the function’s logic, evaluate the entries object. If it is not empty, retrieve the last form entry. If it is empty, indicate that no entries have been submitted yet.
Here is a sample snippet that runs through the process mentioned above:
// Add a new column to the form list
add_filter( 'gform_form_list_columns', 'add_last_entry_submitted_on' );
function add_last_entry_submitted_on( $columns ) {
$columns['last_entry_submitted_on'] = 'Last Entry Submitted On'; // Column details.
return $columns;
}
// Populate the new column
add_action( 'gform_form_list_column_last_entry_submitted_on', 'populate_last_entry_submitted_on' );
function populate_last_entry_submitted_on( $form_object ) {
$form_id = $form_object->id; // Get the form ID from the form object.
$sorting = array( 'key' => 'date_created', 'direction' => 'DESC' ); // Sort by submission date, descending.
$paging = array( 'offset' => 0, 'page_size' => 1 ); // Get only the latest entry.
$entries = GFAPI::get_entries( $form_id, null, $sorting, $paging ); // Get the entries in a form.
if ( ! empty( $entries ) ) {
$last_entry = $entries[0]; // The last entry object.
$last_entry_submitted_on_date = new DateTime($last_entry["date_created"]); // Create a DateTime object with the time string returned from the entry object.
$last_entry_submitted_on_date = $last_entry_submitted_on_date->format('Y/m/d \a\t g:i a'); // Format the date in a more readable way.
echo $last_entry_submitted_on_date; // Display the date.
} else {
echo 'No entries yet'; //In case a form with no entries is listed, indicate no entries.
}
};
The expected result is something like this:
Hey John,
You can try the code provided by @obijuan. But if you prefer to use a smaller amount of code, then try the following block.
add_filter( 'gform_form_list_columns', 'add_latest_entry_column' );
function add_latest_entry_column( $columns ) {
$columns['latest_entry'] = 'Latest Entry Date'; // you can modify the text here
return $columns;
}
add_action( 'gform_form_list_column_latest_entry', 'populate_latest_entry_column' );
function populate_latest_entry_column( $item ) {
global $wpdb;
$form_id = $item->id;
$entry = $wpdb->get_row( "SELECT max(date_created) as max_date FROM {$wpdb->prefix}gf_entry WHERE form_id = {$form_id}" );
if ( $entry && $entry->max_date ) {
$date_string = $entry->max_date;
$date = new DateTime($date_string);
echo $date->format('m/d/Y h:i A');
} else {
echo 'No entries yet'; // this text also can be modified
}
}
The output will be like the following screenshot:
Please add the provided code block in your child theme’s functions.php
file or use the Code Snippet plugin.
Give it a shot, and let me know how it works! I’m looking forward to hearing back from you.
Best Regards
Faisal
Thanks, Juan and Faisal. Both methods work well.
Do either of you know if it’s possible to make the new column sortable, too?
Hi, John.
The short answer is yes, it is possible to make the last column sortable.
The ‘thing’ is that it is a somewhat complex process.
To sort the forms list by the “Last Entry Submitted On” column values, you’d need to create a custom query for the forms list.
The first consideration is that the ‘submitted on’ date is not stored in the form but in the entries.
You are retrieving the ‘submitted on’ date for the latest submitted entry and displaying it in the custom column on the forms list.
Other considerations you need to make are the performance impact this can have and keep in mind the maintenance it may require with future Gravity Forms updates.
To sort the admin column, you will need a significant amount of custom coding. Alternatively, you can try using Admin Columns Pro for Gravity Forms, which can be found here: