If you’ve ever wondered how to get a field’s value by its label in Gravity Forms, this post will help you figure this question out using some PHP code.
Sometimes, you need to send data from your Gravity Forms to a third party. It may be your CRM, ERP, email marketing service, or anything else. In my opinion, the Gravity Forms plugin makes this process much more complicated than its alternatives (Contact Form 7, for example).
Introduction to the problem
There’s a helpful hook in the Gravity Forms plugin you can use to achieve this: gform_after_submission. The authors of the Gravity Forms documentation kindly provided an example of how you can send data from a submitted form to somewhere else.
add_action( 'gform_after_submission', 'post_to_third_party', 10, 2 );
function post_to_third_party( $entry, $form ) {
$endpoint_url = 'https://thirdparty.com';
$body = array(
'first_name' => rgar( $entry, '1.3' ),
'last_name' => rgar( $entry, '1.6' ),
'message' => rgar( $entry, '3' ),
);
GFCommon::log_debug( 'gform_after_submission: body => ' . print_r( $body, true ) );
$response = wp_remote_post( $endpoint_url, array( 'body' => $body ) );
GFCommon::log_debug( 'gform_after_submission: response => ' . print_r( $response, true ) );
}
Source: https://docs.gravityforms.com/gform_after_submission/#h-3-send-entry-data-to-third-party.
As you see, the author of this example uses the rgar() function to retrieve a value of an entry’s field. Basically, this function just looks for a key (the second argument of this function) in the array provided as the first argument. Then it returns a value of this key, a default value (the optional third argument), or null.
The problem
The first question that may appear in your head after looking at the code above: What the heck is ‘1.3’, ‘1.6’, and ‘3’?
Well, the thing is that a Gravity Forms’ Entry object stores its fields as numbers. I don’t know about you, but I get used to retrieving fields’ values using their labels or human-readable IDs.
In most cases, that is not a big deal. You can just check an Entry object and see its structure to grab an ID of a field you need.
One of my clients has a WordPress multisite. On every website of the network, they have a contact form built with Gravity Forms. And they have a custom plugin that takes data from every new entry and sends it to their ERP software via API.
The forms on these websites are very similar, but they are not identical. Although, the data required to send a request to the ERP is the same for all forms. But because forms are independent, their fields have different IDs, which means that in our custom plugin, we can’t use that numeric ID to access the same field for each form. But the good news is that all these fields have identical labels.
Obviously, we had to find a way to retrieve the values of those fields using their labels.
Solution
To achieve this, we’ll use the hook from the example above — gform_after_submission.
As you might have noticed, this hook takes two arguments — $entry and $form. The first one corresponds to an entry just submitted, and the second one corresponds to a form that has been used. The last one will be very helpful for our case.
In the documentation, we can find an example of the JSON structure of the Form object. You can dump the $form variable and your own, and you’ll see something very similar.
The Form object has a property named “fields”. This property contains an array of all fields belonging to this form. Every element of this array, for its part, has two properties that we can use to solve our problem: “label” and “id”.
That’s exactly what we need. Since we know the labels of our fields, we can now retrieve their IDs: all we need to do is map the array.
Let’s write some code
Let’s imagine that this time we need to find values of three entry fields. Their labels are First Name, Last Name, and Email. To retrieve their values, we need to find their IDs first. We can do this following way:
add_action( 'gform_after_submission', 'get_entry_fields_by_their_labels', 10, 2 );
function get_entry_fields_by_their_labels( $entry, $form ) {
// We're going to store every field's ID in a variable to use it later
$first_name_field_id = null;
$last_name_field_id = null;
$email_field_id = null;
// Let's iterate through the "fields" array and find our fields there
foreach ( $form['fields'] as $field ) {
if ( ! isset( $field['label'] ) || ! isset( $field['id'] ) ) {
continue;
}
$field_label = $field['label'];
$field_id = $field['id'];
switch ( $field_label ) {
case "First Name":
$first_name_field_id = $field_id;
break;
case "Last Name":
$last_name_field_id = $field_id;
break;
case "Email":
$email_field_id = $field_id;
break;
}
}
// At this point we already should have the fields' IDs in our variables
}
Okay, we’ve successfully dealt with the most complicated part of our mission. Now, all we need to do is to use the fields’ IDs we’ve just retrieved as a second parameter in the rgar() function that we’ve investigated at the beginning of this post.
Final code
add_action( 'gform_after_submission', 'get_entry_fields_by_their_labels', 10, 2 );
function get_entry_fields_by_their_labels( $entry, $form ) {
// We're going to store every field's ID in a variable to use it later
$first_name_field_id = null;
$last_name_field_id = null;
$email_field_id = null;
// Let's iterate through the "fields" array and find our fields there
foreach ( $form['fields'] as $field ) {
if ( ! isset( $field['label'] ) || ! isset( $field['id'] ) ) {
continue;
}
$field_label = $field['label'];
$field_id = $field['id'];
switch ( $field_label ) {
case "First Name":
$first_name_field_id = $field_id;
break;
case "Last Name":
$last_name_field_id = $field_id;
break;
case "Email":
$email_field_id = $field_id;
break;
}
}
// At this point we already should have the fields' IDs in our variables
$first_name = null;
$last_name = null;
$email = null;
if ( isset( $first_name_field_id ) ) {
$first_name = rgar( $entry, $first_name_field_id );
}
if ( isset( $last_name_field_id ) ) {
$last_name = rgar( $entry, $last_name_field_id );
}
if ( isset( $email_field_id ) ) {
$email = rgar( $entry, $email_field_id );
}
// We've got all the required data. Now we can send it somewhere else
API_Handler::create_new_contact( $first_name, $last_name, $email );
}
I hope that my discovery of how to retrieve a field’s value using its label will be helpful for your project. Feel free to share your opinion about this post and my code in the comments section below.