Using Gravity Forms ‘gform_after_delete_field’ PHP filter

The gform_after_delete_field action hook in Gravity Forms enables you to perform actions right after a field is deleted from a form.

Usage

Use the action hook like this:

add_action( 'gform_after_delete_field', 'your_custom_function', 10, 2 );

Then define your custom function:

function your_custom_function( $form_id, $field_id ) {
    // your custom code here
    return $form_id;
}

Parameters

  • $form_id (integer): The ID of the current form.
  • $field_id (integer): The ID of the deleted field.

More information

You can find additional details on the Gravity Forms documentation for gform_after_delete_field. This action hook is located in the GFFormsModel::delete_field() function in form_model.php.

Examples

Logging Deletions

This example logs every time a field is deleted, recording the username, form ID, and field ID in a log file.

add_action( 'gform_after_delete_field', 'log_deleted_field', 10, 2 );

function log_deleted_field( $form_id, $field_id ) {
    // File to record deletions
    $log_file = ABSPATH . '/gf_deleted_fields.log';

    // Open the log file for appending
    $f = fopen( $log_file, 'a' );

    // Get the current user
    $user = wp_get_current_user();

    // Write the log entry
    fwrite( $f, date( 'c' ) . " - Field deleted by {$user->user_login}. Form ID: {$form_id}. Field ID: {$field_id}\n" );

    // Close the log file
    fclose( $f );
}

In this example, we use the wp_get_current_user() function to retrieve the current user’s information, and then write it to a log file with the form ID and field ID whenever a field is deleted. The log file is stored in the root directory of your WordPress installation.