What is Gravity form?
Gravity form is one of the most popular contact form plugin for WordPress websites.
Generally, we can set the choices of dropdown/selection field from the wordpress dashboard.
But in certain cases, we need to set these choices dynamically from a saparate data source.
add_filter('gform_pre_render_3', 'populate_job_posts');
function populate_job_posts($form) {
foreach ($form['fields'] as &$field) {
if($field->cssClass == 'dynamic_choices'){
$job_posts = get_posts(array(
'post_type' => 'job',
'numberposts' => -1,
'post_status' => 'publish',
));
$choices = array();
foreach ($job_posts as $job_post) {
$choices[] = array('text' => $job_post->post_title, 'value' => $job_post->ID);
}
<span class="php_comment">// Update the placeholder text for the dropdown field.</span>
$field->placeholder = 'Applied For*';
$field->choices = $choices;
}
}
return $form;
}