Prevent Gravity Forms Field from Being Saved in Database

Published by John on August 13, 2018 Under Wordpress

While converting an old PDF form to an online web-form for a client using Gravity Forms on their website, there was a single field in the form that I didn’t want to be saved in the database.

By default, Gravity Forms creates an entry into the database each time someone submits a form, which includes the value of each form field that the person entered when submitting the form. This can be quite useful if you need to go back and see what was submitted or if confirmation email ends up getting lost as spam. However, it also means that anything that a person enters, will be visible in plain text within the database.

I did some poking around, but didn’t see an easy/clean way of preventing a field from being saved in the database, while still ensuring that the field would be visible in the confirmation email. So, I created the below, which updates the field value after a form has been submitted.

This is, of course, less efficient than simply clearing it out prior to save and if anyone has a cleaner way of doing it, feel free to post an update.

The Code

Add the below to your functions.php file. As always, please make sure you backup your files/database prior to making any changes and if you are using the below on a production site or with data that is mission critical, please test prior to use.

Important: The below needs to be customized to your site. Please review instructions below.

/* Mod to prevent a field in the Gravity Forms Intake Form from being saved in database */

add_action( 'gform_after_submission_{THE_FORM_ID}', 'kcr_gf_clear_individual_form_entry' );

function kcr_gf_clear_individual_form_entry( $entry ) {
    global $wpdb;

    /* $field_number_id:
     * The numeric ID of the field found when editing your form.
     * After clicking on the individual field, the ID will appear as follows in the title: {FIELD_NAME}: Field ID {FORM_FIELD_NUMERIC_ID}

    $field_number_id 		= {FORM_FIELD_NUMERIC_ID};

    $lead_id                = $entry['id'];
    $lead_detail_table      = RGFormsModel::get_lead_details_table_name();

    $sql = $wpdb->prepare( "update {$lead_detail_table} set value=%s WHERE lead_id = %d and field_number=%d ", array('XXXX', $lead_id, $field_number_id));
    $wpdb->query( $sql ); 

Before this will work, you need to update two values in the code, replacing the curly brackets {} with a numeric ID:

  • {THE_FORM_ID}: This is the numeric form ID visible in several places within gravity forms, including in the URL as ‘&id={THE_FORM_ID},’ when editing the form next to the name, and also when viewing the list of forms
  • {FORM_FIELD_NUMERIC_ID}: This is the ID of the individual form that you want to clear out. The easiest way to see this is by clicking on the field as though you were editing it. After that, it will show you the FIELD ID

In this example, you would replace {THE_FORM_ID} with ‘1’ and {FORM_FIELD_NUMERIC_ID} with ‘4’:


How it Works

The above function adds an action to the gform_after_submission_1 hook, which ensures it only fires with form #1(or whatever you set as {THE_FORM_ID}.

After the form has been saved and confirmations/notifications submitted, the kcr_gf_clear_individual_form_entry function updates the entry for form field #4, or whatever you set {FORM_FIELD_NUMERIC_ID}, to be ‘XXXX’

Possible Issues with This

The main issue with this, aside from it occurring after the entry was saved in the database and also requiring some user input in terms of setting up the ID of form and field, is that it won’t work with all field types. There is also a get_lead_details_long_table_name function for storing long text values, like textareas.

If you are trying to clear out a long textarea, the above won’t work without modification. You would need to pull the lead_detail record to get the ID and then use that ID to clear out the wp_rg_lead_details_long entry.

No Comments |

Add a Comment