Hello team. Is there a hook to automatically empty the trash after X days? The documentation says that the trahs isn’t emptied automatically, but maybe programatically?
First, create a custom function that will empty the trash for Gravity Forms:
function empty_gf_trash_after_x_days($days = 30) {
// Get all forms
$forms = GFAPI::get_forms();
// Get the current timestamp
$now = time();
// Loop through all forms
foreach ($forms as $form) {
// Get the form ID
$form_id = $form['id'];
// Get entries in the trash for this form
$search_criteria = array(
'status' => 'trash',
);
$entries = GFAPI::get_entries($form_id, $search_criteria);
// Loop through the entries
foreach ($entries as $entry) {
// Check if the entry is older than the specified number of days
$date_created = strtotime($entry['date_created']);
$diff = ($now - $date_created) / (60 * 60 * 24); // Calculate the difference in days
if ($diff > $days) {
// Delete the entry permanently
GFAPI::delete_entry($entry['id']);
}
}
}
}
This function will loop through all forms, get the entries in the trash, and delete them permanently if they are older than the specified days.
Next, schedule a WordPress Cron Job that will execute the function daily:
function schedule_gf_trash_cleanup() {
if (!wp_next_scheduled('gf_trash_cleanup_daily')) {
wp_schedule_event(time(), 'daily', 'gf_trash_cleanup_daily');
}
}
add_action('wp', 'schedule_gf_trash_cleanup');
function run_gf_trash_cleanup() {
// Call the custom function with the desired number of days
empty_gf_trash_after_x_days(30); // Change 30 to the desired number of days
}
add_action('gf_trash_cleanup_daily', 'run_gf_trash_cleanup');
Thanks we have a similar function for archiving entries. I’d think that GF had a core function to schedule a deletion.