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.

Using custom fields to populate hidden fields

  1. bandolero
    Member

    Hello everyone

    I'm new to GF and so far I've been really impressed by the wealth of information available on the support forum and in the documentation. The plugin is brilliant too.

    How ever I've hit a brickwall and after 2 hours of reading I need help.

    I have a heavily customised Wootheme [ Estate ] that's using a live feed of residential and commerical properties. On each property page that's a button that allows you to 'request a viewing' for one of these properties.

    The theme comes with a 'contact-form' template that I started to hack. The out of the box contact form grabs the ID of the property from the querystring and populates an input field. However I need much more information than this so I bought GF.

    What I'd like to do is embed a GF inside the property page - I understand how to do this.

    The form will request fields such as name, email, telephone number etc from the person who's requesting the viewing.

    How ever I'd like to populate several hidden fields with variables that contain the ID of the property, the address of the property and the location of the property and have those fields included in the email that I receive on submission of the form.

    I've got some custom fields set up in the PHP page that displays the property and will have the embedded GF.

    They're set up like this to use in the page

    $property_feed_id = get_post_meta($post->ID,'feed_id',true);

    and

    $property_address = get_post_meta($post->ID,'address',true);

    If the GF is embedded in the page that displays the property, is it not possible to populate some hidden fields with this variables using the GF interface. As that information is already available in the page?

    I've tried clicking 'Allow field to be populated dynamically ' and then in the parameter box tried a combination of adding $property_address / $property_feed_id etc

    Or am I approaching this totally wrong? Should I be trying to post all this information in the querystring and building a form to operate similar to the Woothemes contact form? Is that the most elegant way of doing it?

    Ideally I wanted the form to be available in the property page itself. The software that provides the feed scrapes the inbox where the "request a viewing' submission lands and uses the ID of the property to generate a lead. Hence needing that information. I tried hacking the Woothemes Contact Form and it was turned out to be hassle. I hope GF hits the mark in what I'm trying to do.

    Hope all this makes sense and someone can help or point me in the right direction.

    Thanks in advance
    Mark

    Posted 13 years ago on Thursday February 3, 2011 | Permalink
  2. You are on the right track, but approaching it wrong.

    When you set a field to be populated dynamically, the parameter name isn't code. It's a parameter name that you would then use to populate the field dynamically via the query string or using PHP code.

    For instance, if I had an Email field on my form and I set the Email field to be populated dynamically and give it a parameter name of "email" I could then populate it using the querystring by passing email= in the query string when calling that page. (ex. mydomain.com/mypage?email=VALUE). In this case "email" is the parameter name and is what I use to target that field.

    You are trying to use it to populate it with a variable name, which isn't how you would do this.

    If you want to populate a field dynamically using PHP, which is what you would want to do if you want to populate it with the value of a custom field, you would have to do so with PHP. Here is an example of how you would pre-populate a field using PHP, this example assumes the parameter name for the field is "date".

    <?php
    add_filter("gform_field_value_date", "populate_date");
    function populate_date($value){
    return "10/10/2010";
    }
    ?>

    This example pre-populate the field you have given a parameter name of "date" with the value of "10/10/2010". To do what you would want to do you would return the value of the custom field instead.

    This code would be placed in your theme template file or functions.php depending on how you implement it.

    We can look into adding the ability to set the default value of a field using a custom field key, it's a good idea and makes sense as a built in feature so that you can do so without having to write code.

    Posted 13 years ago on Friday February 4, 2011 | Permalink
  3. bandolero
    Member

    Hi Carl

    Thanks for the swift reply. I think I followed what you mean though I can't get the value of the field to populate using the method described above. I've tried the following.

    propertyid is the parameter name for the hidden field I want to populate dynamically. $property_feed_id is my custom field as mentioned above.

    <?php
    add_filter("gform_field_value_propertyid", "populate_propertyid");
    function populate_propertyid($property_feed_id){
    return $property_feed_id;
    }
    ?>
    <?php
    add_filter("gform_field_value_propertyid", "populate_propertyid");
    function populate_propertyid($value){
    return $property_feed_id;
    }
    ?>

    That would be ace if you were set the default value of a field from a custom field key. I can see them available when creating a field for submission which I think is great.

    Thanks for your help.
    Mark

    Posted 13 years ago on Friday February 4, 2011 | Permalink
  4. Are you setting the value of the variable $property_feed_id somewhere else on your site?

    I ask because WordPress custom fields aren't accessed that way. You have to query the post meta to get the custom field value. If property_feed_id is the key for that custom field, you have to query post meta to get the value for that key. Simply returning $property_feed_id isn't going to work unless you have created the $property_feed_id variable and set it's value equal to the property_feed_id custom field.

    Try this:

    <?php
    add_filter("gform_field_value_propertyid", "populate_propertyid");
    function populate_propertyid($value){
    return "TESTID";
    }
    ?>

    See if that code stores "TESTID" as the value of the hidden field. If it does, then the code above is working fine. It's just a matter of you implementing the write code to return the custom field. Right now it's returning nothing because $property_feed_id doesn't exist as a variable because it's a custom field.

    Here is how you would get the value of a custom field in WordPress using code:

    get_post_meta($post->ID, 'property_feed_id', true);

    In order to get the value of a custom field this way it has to be placed within the loop that displays your post. So it would have to go in your theme template file, not in your functions.php file.

    I haven't tested this but what you want is going to look something like this:

    <?php
    add_filter("gform_field_value_propertyid", "populate_propertyid");
    function populate_propertyid($value){
    return get_post_meta($post->ID, 'property_feed_id', true);
    }
    ?>
    Posted 13 years ago on Friday February 4, 2011 | Permalink
  5. bandolero
    Member

    Hi Carl

    Thanks for the reply and information.

    Yep, you're right about the variables - in my first post I did mention about populating them using get_post_meta which is why I was getting super confused - not to mention calling the GF inside the template file and the function in the template file too - I had read on the codex when modifying the theme about using them inside the loop too as you pointed out - which I thought I was doing. So armed with your information I did some more reading and worked out the following

    add_filter("gform_field_value_propertyid", "populate_propertyid");
    function populate_propertyid($value){
    return get_post_meta(get_the_ID(),'property_feed_id',true);

    I swapped out get_post_meta for get_the_id

    And it worked.

    Many thanks for your help and patience. I'm going to build the rest of the form and go from there and hopefully get it all working.

    Mark

    Posted 13 years ago on Saturday February 5, 2011 | Permalink
  6. Glad to hear you got it working Mark. Gravity Forms does a lot out of the box and can do even more if you put the hooks/filters to work to customize it.

    Posted 13 years ago on Saturday February 5, 2011 | Permalink