Gravity Forms is my new favourite plugin and for good reason. It is truly limitless and is the best plugin in its field.
With that said, I thought I’d share how I recently used it to build a form that would be pre-populated with the user’s information (like name, email address, shipping address and phone number) so that they could place an order for marketing materials to be sent to them.
Collect the information
If you want to use fields that are already included in the standard WordPress user profile, like name, or email address, then you’ll just need to use a function to pull that information from an existing database entry. However, if you want to collect extra information, such as an address, shipping preference or really, anything you want, then you’ll need to create extra fields where the user can enter this data.
Collect new data
In order to collect new data, we can write a function to add a new section to the User Profile page, where people can enter their information. I’m going to show you using the code that I wrote for the project that I was working on when I needed this.
This code adds the new section to the page and creates several different fields for all the information that we want to collect. Each field has a unique identifier, so that we can pull each piece of information in to the form separately.
This function should be pasted into your functionality plugin, or functions.php if you don’t have one (in which case, I suggest you spend 10 minutes and create one).
In the first function, we created a table to display on the page, and each line in the table has a unique identifier for the field (such as address_line_1 or full_name), displays the existing value from the database if there is one and displays a description to help the users fill out the fields.
The second function saves the information to the database, but only if the user has the privilege to do so, as an added security measure.
It should be easy to deduce how the code works and has been replicated several times over to create all the fields, so cut and edit what you need to save the information as you need it.
Populate the form with the data
Now that you’ve collected the information from your users, you want to set up your Gravity Form so that it pulls the information from each user’s profile when they pull up the form.
You can do this by creating special filters that Gravity Forms recognises. All these filters start with “gform_field_value_” and then have the same unique identifiers that we created in the first step. For example, the filter name for the full_name field is gform_field_value_full_name. Copy the code below and adjust all the filters for the specific fields that you created in the function above. This code goes in the same functions.php file or functionality plugin that we used above.
Configure Gravity Forms
Everything is now set up to have Gravity Forms receive the data, but you need to tell it where you want this data to appear.
So start off by either creating a new form, or editing an existing one. Find the fields where you want the data to appear and edit them. Click on the Advanced tab and check the box for Allow this Field to be Populated Dynamically. In the field labeled Parameter Name, enter the unique identifier that we created in the first function and then reused in the second function. In this example, the parameter would be full_name.
Save your form and try it out. Go to the form and see whether it pulls in your information (make sure that your profile contains the information you’re trying to fill the form with, especially if you created custom fields).
What unique ways have you used this trick to fill out forms and save some time? If you’re having trouble getting this to work, let me know and I’ll see if I can help you.