Using Gravity Forms ‘gform_before_delete_form’ PHP action

The gform_before_delete_form is an action hook in Gravity Forms that allows you to execute custom code right before a form is deleted.

Usage

This action hook can be used as follows:

add_action( 'gform_before_delete_form', 'my_custom_cleanup_function' );
function my_custom_cleanup_function( $form_id ) {
    // your custom code here
    return $form_id;
}

Parameters

  • $form_id (integer): The ID of the form that is about to be deleted.

More Information

See Gravity Forms Docs: gform_before_delete_form

This action hook is implemented in Gravity Forms 1.0. It is not deprecated and is located in the delete_form() function of the form_model.php file.

Examples

Logging Form Deletion

This example logs form deletions by writing an entry to a log file each time a form is deleted.

add_action( 'gform_before_delete_form', 'log_form_deletion' );
function log_form_deletion( $form_id ) {
    // Define log file location
    $log_file_path = ABSPATH . '/gf_deleted_forms.log';
// Open the log file in append mode
$log_file = fopen( $log_file_path, 'a' );

// Get current user data
$current_user = wp_get_current_user();

// Count the entries in the form
$form_entries_count = RGFormsModel::get_lead_count( $form_id, '' );

// Write the log entry
fwrite( $log_file, date( 'c' ) . " - Form deleted by {$current_user->user_login}. Form ID: {$form_id}. Entries: {$form_entries_count}\n");

// Close the log file
fclose( $log_file );

}

In this example, the log_form_deletion() function is hooked into gform_before_delete_form. When a form is deleted, the function is triggered. It opens a log file, retrieves the current user information, counts the number of entries in the form, and writes this information to the log file. Finally, it closes the log file.