Custom field IDs for Gravity Forms

If you haven’t had the chance to work with it before, Gravity Forms is pretty fantastic. I was first turned onto it a few years ago while I was at Buckeye Interactive, where it was a mainstay across most of our client sites. Besides presenting an easy-to-manage interface for building forms, the plugin also makes good use of the WordPress Plugin API (thus making my life way easier) and has a vibrant ecosystem of official and unofficial add-ons.

One area where Gravity Forms could stand to improve, however, is making it easier to identify fields. Let’s say, for example, we have a form where we’re collecting a name and an email address; outside of assuming that the regular text field is the name and the input[type="email"] is the email address, Gravity Forms doesn’t really have a straight-forward way to identify fields when you’re doing extra work with submissions (like sending them to a newsletter or a CRM system).

In my new role as Director of Technology at Growella, one of the first things I needed to figure out was how we could reliably map Gravity Forms submissions into third-party tools.

Assigning custom IDs to fields

Fortunately, Gravity Forms lets us add custom properties to our fields; knowing this, we can easily add a “Field ID” input, which lets us reference a field by this ID:

This function will generate the Field ID input markup on the gform_field_advanced_settings action, calling Gravity Forms’ setFieldProperty() function whenever the input changes.

Next, we need to tell Gravity Forms that fieldID is a valid property:

This will also pre-populate the input’s value with a field ID, if one has previously been saved.

A Gravity Forms field with a custom "Field ID" input

That’s it! With these two functions, we’re able to assign custom field IDs, removing a lot of the headache of mapping to third-party services.

Mapping Gravity Forms fields to third-party services

Having the custom field IDs is great, but the icing on the cake is being able to easily get any fields with custom IDs to send to your third-party services. Thanks to your friends at Growella, now you can:

In almost any Gravity Forms callback (for example, gform_after_submission), we receive two arrays: $entry and $form. By passing these directly to growella_get_mapped_fields(), we can quickly retrieve non-empty values for fields we’ve assigned IDs to. For example:

Now, go forth and integrate Gravity Forms with :allthethings:!

8 comments on "Custom field IDs for Gravity Forms"

  1. Andy 10 months ago

    Great post, thanks for sharing your code; I was having the very same problems pulling out and sharing data collected from forms and this code looks like a great starting point!

    Many thanks!

    • studio4 10 months ago

      Just in case anyone else is interested in this if you want to add the fieldID to the front-end form as a class you can use the gform_field_css_class hook:

      /**
      * Show mapped field ID in front-end forms for CSS and JS manipulation
      * @param [type] $classes existing classes string
      * @param [type] $field field object
      * @param [type] $form form object
      * @return [type] new classes string
      */
      function growella_gform_custom_class( $classes, $field, $form ) {

      if ( $field->fieldID ) {
      $classes .= ‘ ‘.$field->fieldID;
      }
      return $classes;
      }
      add_filter( ‘gform_field_css_class’, ‘growella_gform_custom_class’, 10, 3 );

  2. Mike 10 months ago

    Perfect – just what I needed! Cheers :)

  3. Jonathan 7 months ago

    are we placing this in the theme functions.php file or a specific php file in the gravity forms plugin folder?

    • Steve 7 months ago

      This would be in your theme’s functions.php file — you should never have to modify the code within wp-content/plugins/gravity-forms, as that would be wiped out with the next update.

      • Jonathan 7 months ago

        Thanks Steve. I had some cache issues on my end and wasn’t seeing the update. i’m all set though now.

  4. lukecav 6 months ago

    Awesome share, thanks for this.

  5. Cristian W. 5 months ago

    I just wanted to say thank you. This solved my problem.

Leave a Reply to Mike Cancel reply