PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Pre-populate form with user data

  1. Sorry if I'm missing this somewhere.

    We have an installation with a large number of subscribers. From just a user work flow perspective, I'd love to pre-populate the name and email fields of our contact forms, if they are logged in.

    Is there a way to pull logged in user details into the form?

    Thanks!

    Posted 13 years ago on Tuesday November 30, 2010 | Permalink
  2. Not automatically. You would have to use available API hooks to pre-populate fields using PHP.

    To populate a field dynamically you first need to set permissions on that field so that it can be populated dynamically.

    - Edit your form
    - Edit the field you want to populate dynamically
    - Select the Advanced tab
    - Click the "Allow field to be populated dynamically" checkbox
    - Enter a parameter name (this parameter name is what you then use to populate the field. An example would be use a parameter name of email for an Email field, etc.).
    - Repeat for each field you want to populate dynamically

    Once you have set your fields up to be populated dynamically you can now either populate those fields either by passing the value via the querystring, or by adding some custom PHP code to your theme.

    To populate a field using the querystring you would simply pass a value to that page via the querystring using the parameter name for the field you want to populate. For example, if you gave an Email field the parameter name email you could populate it like so:

    mydomain.com/myform?email=john@doe.com

    To populate a field using PHP you would add a code snippet to your themes functions.php file and use the gform_field_value_$parameter hook. It looks like this (using a parameter name of email as an example):

    <?php
    add_filter("gform_field_value_email", "populate_email");
    function populate_email($value){
    return "john@doe.com";
    }
    ?>

    You would do the above for each field you want to populate dynamically, making sure you change the code so it uses the appropriate parameter name you setup when setting fields to be populated dynamically.

    The code you would need to use would be slightly different because you wouldn't be returning a hardcoded value, you would have to change it to return the value you want to return... in your case the users email or name. You'd have to get this data from WordPress.

    If you need help implementing this I can refer you to WordPress developers that have Gravity Forms customization experience.

    Posted 13 years ago on Tuesday November 30, 2010 | Permalink
  3. That makes perfect sense! Thanks! (I am fast becoming that WordPress developer with Gravity Forms customization experience).

    Appreciate it.

    Posted 13 years ago on Tuesday November 30, 2010 | Permalink
  4. I managed to get this working by getting the user's user meta data for a field, but I guess the way to do this for multiple fields is to make an array of fields and their corresponding (meta) data? Would that be possible? A code example would be very much appreciated :-)

    I've come this far:

    add_filter("gform_field_value_company", "populate_company");
    function populate_company(){
    global $current_user;
    get_currentuserinfo();
    $id = $current_user->ID;
    $comp = get_user_meta($id, 'company', true);
    return $comp;
    }

    Further more, I'm wondering if it is also possible to use this method to edit a post that has been added through a GF by the current_user?

    Thanks in advance!

    Posted 13 years ago on Wednesday December 22, 2010 | Permalink
  5. Is the "Parameter Name" and the "Field Label" the same thing?

    I'm trying to have the Quantity ordered of a product automatically be filled by the number of participants for an event registration.

    Thanks for the help.

    http://mollermarketing.com/spring-fling-registration-form/ (this is a temporary testing ground for the form)

    Posted 13 years ago on Tuesday January 11, 2011 | Permalink
  6. mmtrav
    Member

    To get around this problem I made sure that my forms that required user registration were not available to users forcing them to login first. Then I used the following code snippet to get my email address populated as this was necessary for notifications:

    add_filter("gform_field_value_Email", "populate_email");
    function populate_email(){
    global $current_user;
    get_currentuserinfo();
    $useremail = $current_user->user_email;
    return $useremail;
    }
    Posted 12 years ago on Sunday October 30, 2011 | Permalink
  7. Thanks for updating this topic with your solution.

    Posted 12 years ago on Sunday October 30, 2011 | Permalink