Pre-populate Fields using Gravity Forms

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.

You don’t really even need to do all of this. Just go to the Form Editor, click the Advanced tab on the field you want to prepopulate, and then under “Default Value” enter {user:user_email} – this is a built in feature (maybe not when the post was originally published here but I believe so) that requires no custom coding. You can also do {user:anymetafieldkey} for any other information you store along with a user’s profile.

Thanks Dave for the quick reply, i will try it! As i see from the code i just need the second snippet as the first one is only for adding these extra fields: address, city, state, zip code, country, phone.

Simone, to the best of my knowledge and from the research I’ve done, this isn’t possible because Gravity Forms uses a PHP uploaded and the form doesn’t know the file size until the upload is complete. There is talk of them revamping that functionality, but I haven’t seen anything on it yet.

I’m not sure what you’re wanting to save? The user profile already manages the user’s name, email address etc. and this allows you to add extra information to their profile, which you can then pull into a Gravity Form.

You’re missing a closing double quote on the value attribute. Start there and see if that does it. Also, not sure if the way you pasted your code cut off the opening php tag, but make sure that’s in place as well, since you have a closing tag (?>) in there.

This looks great! Here’s a question for you- I am using CIMY extra fields for additional bio form fields (wish I’d found your tutorial first)- you can pull extra field info by using something like this:

$value = get_cimyFieldValue(user_id, field_name);

DO you think its possible to use this with gravity forms to prepopulate the fields?

Another quick question for you: using your custom user profile fields created above, would it be possible to hide or show a message or link based on if there is a value in the profile field for the current user?

Yes, I believe so. You could create a hidden field that pulls in the information from theit user profile and then create the message/link that you want to show or hide and set the conditional logic such that it only shows/hides if the field is or is not blank.

Great post! the “__get” magic method for accessing custom user meta was particularly enlightening. Here is another take on how to pre-populate a form from user metadata without having to add a filter for each field.

Use the gform_pre_render filter to target each form to prepopulate by appending the form id to the end of the filter. The prepopulate function just looks for cases where the field’s inputName is not blank (i.e. the form field has been set up for pre-population), and then assumes that the inputName matches the user meta field name.

Another though – if you wanted to populate a form from multiple sources (i.e. not just custom user metadata) then you could prefix each inputName with a data source indicator and then use this to determine what source to grab the inputName from).

Yes, we are going to have an external MS SQL database that will hold the records of each customer. I want to use this type of system you created so that when they enroll on the website, they can input their acccount number and then the form will prepopulate with the information we have on them already. From there we can let them register.

For example you could prefix the inputName with an identifier that indicates where to get the data from to pre-populate. e.g. Use a double underscore to separate the prefix from the inputName – customusermeta__address_line_1, customusermeta__address_line_2, postmeta__productsku, postmeta__productname, etc.

Then you could explode(“__”, $field[“inputName”]) and check the first value in the resulting array to see where to get the data from. If postmeta then populate the $_GET by getting metadata from a post (whose id you would have to get from somewhere like a $_GET set in the URL), and if customusermeta then use $current_user.

I’m on a tablet right now but will try to post a link to some sample code later.

Thank you Brad, looking forward to seeing the sample code. Your example looks really fantastic and it seems to use the same logic that i’m trying to accomplish. In your case, where is your data being pulled from? So happy I found Dave’s thread and your extra input. I finally feel like I can wrap my mind around around gravity form’s functions. Really appreciate you guys taking the time.

Ok, so I just whipped some sample code together (although I didn’t test it). This populates from “usermeta”, “personmeta” or “organizationmeta” depending on the inputName prefix, and uses a double underscore delimiter to separate this prefix from the meta field name. Usermeta is populated from the currently logged in user’s information, personmeta comes from custom fields for posts of type “people”, and organizationmeta comes from fields for posts of type “organizations”. It checks the post type too, so the fields are only pre-populated if a post of the proper type is being viewed, and form “25” (replace with your form ID) can be reused for multiple post types. You can find the code here:

Wondering if anyone has a quick example of how to do this with a custom post type and custom field.
I have a gravity form that submits part of it’s data to create a user profile (which this tutorial explained very easily), and parts of the same form submit to a custom post type.

Ideally, when a user logs into the site and visits the same form, I would like to pre-populated the fields with data from the users profile and from the custom post type field(s). Then the user could change existing values and hit update and it would update the profile and the custom post associated with the profile.

I was able to do this with the custom profile fields but not sure how I would do it with a custom post type and custom meta field.

This will work on a custom post type just as easily as on a page or post. I’m confused what you mean in your second paragraph however, where you say that you want the form pre-filled with info from the custom post type fields. Adding information from the user’s profile will work the same, but I’m not sure what you mean by pulling in information from the custom post type fields. Why would you need to do that?

Hi Dave, because I have the same question, I will respond for him, he had probably created a CPT form before , people submitted some data there and he wants to create another Custom post type form which will use the data from previous CPT form,
Example: I have Custom post type for girls named “portfolio”, girls insert their data as name, surname, country, city , age, weight, height ecc.
When they submit the form it creates a new portfolio page with their data, the same girl will want to ask for a job, so she comes to fullfill another Custom post type form for example “job” with questions what the job should looks like, because the second form she would use more times I would like the logged in girl save from inserting the same data as she has already inserted in the portfolio form which she will submit only once, so I want the wordpress to recognize her email or her user ID and to fullfill some portfolio data fields for her.

I apologise. I thought this comment was for a different tutorial. The changes that you’d have to make would involve pulling in the BuddyPress information instead of the WordPress profile information as I demonstrated. Again, I haven’t worked with BuddyPress, so I don’t know how to do that off-hand.

For somebody who wants also pulling the buddypress profile fields I got that working with this code: `add_filter(“gform_field_value_city”, “populate_city”);
function populate_city($value){
global $current_user;
get_currentuserinfo();
return xprofile_get_field_data(‘city’, $current_user->ID);
}` and parameter name = city

I have populate a dropdown dynamically using the gform_pre_render hook. I have multi page form. The dropdown gets its choices populated dynamically on page 2 of the form. But when I try to access the selected value from the dropdown on the page 4 onwards of the form, I get blank value in rgpost for that field.

However If I try to access the selected value of the dropdown on page 3 (next to the page where dropdown is) I can access the selected value using the rppost.

Hey thanks for this. I’m having a little issue. I created a custom functionality php file and loaded it into the plugins section of my wordpress. I started populating the php while using dreamweaver with the two separate sections of code above and upon adding the section section it keeps giving me a php errors with this line = “return $current_user->;__get($meta_key);” and its the semicolon in between the right angle bracket and the underline before get($meta_key). Anyone else having problems with this?

Hi Dav, I have make one booking form for all of my package in one of my website. In that booking form there is one package field name and other field is also there. but that package field name should automatically come accordingly to package of the page i clicked that booking button from any page .I’m new to wordpress and don’t know what to do. please help me..

The best thing to do in that situation is use query strings. In the form, under “allow field to be populated dynamically” set the parameter name to “package”. Then, from your package pages, just send them to the booking form URL and attach the query string for package. For example, if one of your packages has the value of “premier”, just set the URL to http://www.example.com/booking-form/?package=premier and then that field will be auto-selected.

Wish your site was as fast as this?

Do It With WordPress is proudly hosted by WP Engine, the very best WordPress hosting that money can buy.

Aside from being blazing fast, it's very secure, has a staging area to test changes before making them live, automated daily backups, malware scanning, stellar support from WordPress experts... It's just everything that you would want from your host, and more.