Using Gravity Forms gform_field_input to pass variables from a post to a form

by | 2 Nov, 2020 | How To

Gravity Forms gform_field_input

Gravity Forms is a brilliant form system for WordPress and provides a lot of functionality and can be extended to provide additional custom functionality for your end-users and gform_field_input extends the capability of your forms.

NOTE: This is a pretty advanced tutorial so only knowledgeable developers familiar with WordPress will be able to execute it. In this tutorial, we will be looking at editing the functions.php file and adding some code to it in order to manipulate the hidden field and the HTML field on the contact form. So we will assume that you already know how to create a Gravity Form and edit the functions file with PHP using Gravity Forms gform_field_input.

The link to the docs in Gravity forms for gform_field_input is: https://docs.gravityforms.com/gform_field_input/

In the screenshot below, we are showing a button on a page/post which invites the user to make an offer on a domain name. When the user clicks the button it passes a variable to the Gravity Form which translates the variable into the page title, in our case the domain name.

The button will have something like this on it: /contact/?domain_id=999

Gravity Forms gform_field_input

When the user clicks the button it shows on the Gravity Form as below.

This adds a bit of a friendlier way of allowing the end-user to know what they are applying for.

Gravity Forms gform_field_input

The kicker here is also that we can use the same Gravity Form as the main contact form, so you can essentially have one form for everything, general contact form as well as the products contact form.

So, if the user comes to the contact page WITHOUT passing a variable, the section above will not be shown to the end-user.

So, let’s get started.

We will assume you have already created a Gravity Form with the usual contact fields.

We will start by adding the custom fields and then apply the code to the buttons.

STEP 1 – Add Fields to Your Gravity Form

You need to add two fields to your Gravity Form in order to trigger the gform_field_input function.

Field 1 is an HTML CONTENT field.
Field 2 is a HIDDEN field.IMPORTANT, as you add the fields, make a note of the field ID’s as you will need them in future steps.

1. HTML CONTENT FIELD
You don’t need to add anything in the advanced tab as the functions.php will handle it all.

HTML Content Field

2. HIDDEN FIELD
You don’t need to add anything in the advanced tab as the functions.php will handle it all.

Hidden Field

STEP 2 – Add gform_field_input to functions.php

Now the fields are added to the Gravity Form, we can save the form and open the functions.php file to make the magic happen.

Gravity Forms gform_field_input will handle the incoming request by parsing the id and outputting the result.

Our code below is going to do THREE main things.

1. It will detect if you have sent a variable called “domain_id” which will be on your button like this: /contact/?domain_id=999

Gravity Forms gform_field_input

2. It will detect if the HTML CONTENT field exists and if so will proceed to display the domain name on the page using HTML. The cool part is, if no variable is detected then this section won’t show on the Gravity Form.

Gravity Forms gform_field_input

3. Like Item 2, but in this case, it will detect the hidden field and populate it with the domain name so that when the form is sent, the domain name is included.

Gravity Forms gform_field_input

STEP 3 – Prepare your button

In our case, we simply passed the post_id of the post to the contact page and then let the functions.php handle the generation of the page title, in this case, a domain name.

You can use pass anything you like, but for good practice, we suggest allowing only numerical variables as you might have some security issues with XSS.

BETTER: /contact/id=999
NOT GOOD:  /contact/?domain=xwz.com

Here is our code for the functions.php

For more interesting Gravity Form snippets, click here: https://www.jucra.com/whmcs/index.php?rp=/knowledgebase/tag/gravity-forms

Craig Edmonds

Post Written by Craig Edmonds

Craig co-owns JUCRA Digital and brings a rich background in hospitality and finance. Transitioning from finance, he embarked on a sabbatical to Marbella, Spain in 2000 and has since made a significant shift into web design and wordpress development. Residing in Marbella ever since, Craig thrives on the dynamic challenges of the internet, has a strong affinity for WordPress, and is an enthusiast of Cpanel.

Posted in: