In some cases I need to delete an entry as soon as it’s been saved and certain elements processed. I use the hook gform_after_submission to do what needs to be done and then at the end of the function call
GFAPI::delete_entry( $entry_id );
This works (the entry does get deleted) but it then produces a critical error. The first part of the long error message (with WP_DEBUG turned on) is:
Uncaught Error: Cannot use object of type WP_Error as array in /var/www/html/cesuexperts.uri.edu/wp/wp-content/plugins/gravityview/includes/extensions/edit-entry/class-edit-entry.php:373
What I suspect is going on is the Gravity View code is looking for the entry to do something more with it and not finding it.
Does anyone have any ideas how to get around this? I tried using the hook gform_post_process and that produced the same error.
The problem you’re encountering is that Gravity View tries to work with an entry you’ve already deleted. To fix this, we can modify your code slightly so that Gravity View can access the entry before it gets deleted.
Here’s a modified version of your code that should help:
// First, we'll add our custom function to run after the form submission
add_action('gform_after_submission', 'process_and_delete_entry', 10, 2);
function process_and_delete_entry($entry, $form) {
// Do the necessary processing with the entry here
// Schedule the entry deletion to happen just before it's actually deleted
add_action('gform_delete_lead', 'delete_the_entry', 10, 1);
function delete_the_entry($entry_id) {
GFAPI::delete_entry($entry_id);
}
// Now, let's trigger the deletion process
do_action('gform_delete_lead', $entry['id']);
}
This code will first let Gravity View work with the entry and then delete it afterward. If you still encounter issues, you can try adjusting the priority of your action by changing the number 10 to a higher value, like 100:
Thank you all. I wasn’t sure if this was truly a bug. It sounds like it is. For the moment I’ve decided to just move the entries to the trash rather than delete it, but it’s good to know that the delete option is available.
And thank you, Faisal, for the work-around. That’s a good solution to have in my back pocket.