urosevic icon

WordPress & GravityForms: Delete Entry After Submission

urosevic | PRO | 07/05/15 08:08:37 AM UTC | 0 ⭐ | 627 👁️ | Never ⏰ | []
PHP |

1.17 KB

|

None

|

0 👍

/

0 👎

<?php
 
add_action( 'gform_after_submission', 'au_gfapi_delete_entry_gform_after_submission' );
/**
 * Prevents Gravity Form to keep in the database stored entries
 * for a specific forms. In this example for forms with ID 2 and 3
 *
 * @param array $lead  Array of lead data from submitted form.
 */
function au_gfapi_delete_entry_gform_after_submission( $lead ) {
 
    // Define GravityForms form ID's for which we don't need saved entries
    $forms_to_skip = array(2, 3);
 
    // Check do we have matching form ID
    if ( in_array($lead['form_id'], $forms_to_skip) ) {
 
        // Check do we have class GFAPI (just in case)
        if ( class_exists('GFAPI') ) {
 
            // Delete entry - proper safe approach w/o manual messing with DB
            $ret = GFAPI::delete_entry( $lead['id'] );
 
            // Maybe we wish to do some error reporting on success?
            if ( WP_DEBUG && $ret === true ) {
                error_log("Entry #{$lead['id']} for form {$lead['form_id']} has been deleted!");
            }
 
            // Free some memory
            unset($ret);
 
        } // END if ( class_exists('GFAPI') )
 
    } // END if ( in_array($lead['form_id'], $forms_to_skip) )
 
} // END function au_gfapi_delete_entry_gform_after_submission( $lead )

Comments