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.

Allow fields to be dynamically populated - Agentpress 2.0

  1. I am working with G Forms in conjunction with agentpress 2.0. I notice that in the demo there is a form that asks the user which property they are inquiring about. Can I populate that list dynamically with all the listings in the site so not have my client manually populate it? This is the reason I bought G Forms... Otherwise I could have done the forms with a free module. Hope this is possible.

    Can this be done?

    Posted 11 years ago on Monday November 19, 2012 | Permalink
  2. To populate any form field dynamically, you can use the gform_pre_render hook:
    http://www.gravityhelp.com/documentation/page/Gform_pre_render

    There are examples on that page of populating a drop down custom posts, which is what I think you want to do?

    Posted 11 years ago on Monday November 19, 2012 | Permalink
  3. David Peralty

    Out of the box, there is no way to have all of the WordPress post titles to pre-populate a drop down selection in one of our forms. Though, with some custom PHP, this can be accomplished.

    Posted 11 years ago on Monday November 19, 2012 | Permalink
  4. Thanks Chris. This is helpful. I see in the first example that populates a drop down they use the business category. I am not sure if you or anyone else is familiar with agentpress 2.0 but it is a real estate template that has (i am guessing) a custom data type called listings that doesn't fall into any categories... I am not able to assign a category to listings. Any idea how I would specify that I want to list the listings rather than a category..? Any documentation in the right direction or schooling you may want to bless me with is greatly appreciated. I am not familiar with these hooks and how to utilize them.
    Thanks

    Posted 11 years ago on Tuesday November 20, 2012 | Permalink
  5. Instead of posts in a specific category, you would use posts of a certain post type, without the category. In the first example on the gform_pre_render page, for line 14:

    [php]
    $posts = get_posts("category=" . get_cat_ID("Business"));

    You would use this instead

    [php]
    $posts = get_posts( array( 'post_type'  => 'houses' ) );

    (Replace 'houses' with whatever the actual name of the custom post type is. If you used 'post' there, you would get posts from all categories.)

    Full documentation for the WordPress function get_posts can be found here:
    http://codex.wordpress.org/Function_Reference/get_posts

    Posted 11 years ago on Tuesday November 20, 2012 | Permalink
  6. OK I am much closer but still not successful...

    - I am filtering all other forms

    if($form["id"] != 2)
           return $form;

    - I have specified the post type as listing:

    $posts = get_posts( array( 'post_type'  => 'listing' ) );

    - The code is placed in the function.php file at the root of the theme

    - A form with an id of 2 is placed in the listing and has 3 dummy options.
    Does the form need to be empty?

    Not sure why this isn't working?
    Any help?
    Thanks

    Posted 11 years ago on Tuesday November 20, 2012 | Permalink
  7. I don't think we have enough information to help you. Can you please paste all the code you are using to populate your drop down, at pastebin.com or pastie.org, so we can see about debugging it?

    Posted 11 years ago on Wednesday November 21, 2012 | Permalink
  8. add_filter("gform_pre_render", "populate_dropdown");
    
    //Note: when changing drop down values, we also need to use the gform_admin_pre_render so that the right values are displayed when editing the entry.
    add_filter("gform_admin_pre_render", "populate_dropdown");
    
    function populate_dropdown($form){
    
        //only populating drop down for form id 5
        if($form["id"] != 2)
           return $form;
    
        //Reading posts for "Business" category;
        $posts = get_posts( array( 'post_type'  => 'listing' ) );
    
        //Creating drop down item array.
        $items = array();
    
        //Adding initial blank value.
        $items[] = array("text" => "", "value" => "");
    
        //Adding post titles to the items array
        foreach($posts as $post)
            $items[] = array("value" => $post->post_title, "text" => $post->post_title);
    
        //Adding items to field id 8. Replace 8 with your actual field id. You can get the field id by looking at the input name in the markup.
        foreach($form["fields"] as &$field)
            if($field["id"] == 8){
                $field["choices"] = $items;
            }
    
        return $form;

    - I have it placed at the bottom of functions.php

    - Form's id is 2

    Posted 11 years ago on Wednesday November 21, 2012 | Permalink
  9. Hi, did you change field id to match your ?

    if($field["id"] == 8)

    cause in my form I have field id =4, I just copied code from that thread and everything works

    Posted 11 years ago on Wednesday November 28, 2012 | Permalink