Advertisement
urosevic

WordPress & GravityForms: Delete Entry After Submission

Jul 5th, 2015
283
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. add_action( 'gform_after_submission', 'au_gfapi_delete_entry_gform_after_submission' );
  4. /**
  5.  * Prevents Gravity Form to keep in the database stored entries
  6.  * for a specific forms. In this example for forms with ID 2 and 3
  7.  *
  8.  * @param array $lead  Array of lead data from submitted form.
  9.  */
  10. function au_gfapi_delete_entry_gform_after_submission( $lead ) {
  11.  
  12.     // Define GravityForms form ID's for which we don't need saved entries
  13.     $forms_to_skip = array(2, 3);
  14.  
  15.     // Check do we have matching form ID
  16.     if ( in_array($lead['form_id'], $forms_to_skip) ) {
  17.  
  18.         // Check do we have class GFAPI (just in case)
  19.         if ( class_exists('GFAPI') ) {
  20.  
  21.             // Delete entry - proper safe approach w/o manual messing with DB
  22.             $ret = GFAPI::delete_entry( $lead['id'] );
  23.  
  24.             // Maybe we wish to do some error reporting on success?
  25.             if ( WP_DEBUG && $ret === true ) {
  26.                 error_log("Entry #{$lead['id']} for form {$lead['form_id']} has been deleted!");
  27.             }
  28.  
  29.             // Free some memory
  30.             unset($ret);
  31.  
  32.         } // END if ( class_exists('GFAPI') )
  33.  
  34.     } // END if ( in_array($lead['form_id'], $forms_to_skip) )
  35.  
  36. } // END function au_gfapi_delete_entry_gform_after_submission( $lead )
Advertisement
RAW Paste Data Copied
Advertisement