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 a dynamically generated dropdown based on post title

  1. Hi,

    A client website I am working on has a job application form built using Gravity Forms. This form is displayed only on a WordPress custom post type called 'careers'.

    The form has a dropdown list of all currently available careers, which when selected changes the form options using conditional logic.

    Rather than someone having to manually edit this dropdown list, my client would like it to be pre-populated from all currently published 'careers' custom posts on the site. They would also like an item in the dropdown to be pre-selected, so that the selection matches the careers custom post title the form is currently being displayed on.

    I have just implemented the following for the first requirement and it works great:
    http://www.gravityhelp.com/documentation/page/Dynamically_Populating_Drop_Down_Fields

    How would I extend this function to pre-select an option from the dropdown that matches the current custom post title?

    Thanks in advance!

    John

    Posted 11 years ago on Friday March 15, 2013 | Permalink
  2. How are the forms getting embedded into the post? And the post title will match the career exactly? So, you have a post called "Butcher" and there is a form embedded in it, and you want the career drop down to be pre-selected "Butcher"?

    I guess it matters how the form is being embedded into the post to determine what is the easiest way to do this.

    Posted 11 years ago on Sunday March 17, 2013 | Permalink
  3. Hi,

    The form has been inserted by my client onto the page using the shortcode function in the text editor toolbar, though if this is an issue I could probably put it into the template, as the form is going to be the same on all pages. I was just trying to give my client as much control over when to display the form as possible.

    Yes - the plan is for the post title to match one of the options in the drop-down exactly, and if they do match, then the drop-down option to be automatically selected.

    Thanks

    John

    Posted 11 years ago on Monday March 18, 2013 | Permalink
  4. It will be much easier if you can put it in the template, especially if it is the same on every post, and is just inserted manually/identically every time. If that's possible, we can come up with the code to pre-select that drop down option.

    You could do it with the gform_field_value_your_parameter filter: http://www.gravityhelp.com/documentation/page/Using_Dynamic_Population#Hooks

    Or you could put the value right in the shortcode if you embed the form like this:

    [php]
    global $post;
    $title = $post->post_title;
    echo do_shortcode('[gravityform id=1 field_values='dropdown_field=$title']');

    Something like that.

    Posted 11 years ago on Monday March 18, 2013 | Permalink
  5. Thanks for this - am going to be working on it next week so I will let you know how i get on.

    Posted 11 years ago on Sunday May 12, 2013 | Permalink
  6. Thanks for pointing me in the right direction with this. I'm posting an example of my code here in case others need this solution:

    I went into the advanced tab of the relevant field (the dropdown list) and ticked 'Allow field to be populated dynamically' and gave the parameter a name. Then I opened up the relevant template and added this code at the location where I wanted the form to appear on the page:

    $title = $post->post_title;
    echo do_shortcode('[gravityform id="1" field_values="dropdown_field='.$title.'"]');
    Posted 11 years ago on Monday May 13, 2013 | Permalink
  7. David Peralty

    Glad you got it sorted out. I hope it helps others. Thanks for posting your solution.

    Posted 11 years ago on Monday May 13, 2013 | Permalink