All of the tutorials I am seeing for dynamically populating a drop down aim to replace the drop down options with a set of data (like adding certain posts to the drop down). What I am trying to do is dynamically choose an option from the drop down based on previously entered user information.
For instance, I have set up a custom user field for country. When the user registers, they choose the country they are in from a drop down. I am trying to have my gravity forms automatically choose the country that they selected upon registration, but leave the defined drop down options available. I have been able to dynamically populate text fields with custom user information with a function like this:
// populate the field with "user_title" as the population parameter with the "user_title" of the current user
function env_populate_title() {
$s2title = get_user_field ("title");
if ($s2title == '') { $value = populate_usermeta('user_title'); }
else { $value = $s2title; }
return $value;
}
add_filter('gform_field_value_user_title', 'env_populate_title' );
I know I need to pre-render the form to apply the drop down value but I'm not sure how to just select the value and not replace the drop down choices. I know that this is not there yet but it should be something similar to this:
function env_populate_country($form) {
foreach($form['fields'] as &$field):
if($field['type'] != 'select' || strpos($field['cssClass'], 'country') === false):
continue;
else:
$choices = get_user_field ("country");
$field['choices'] = $choices;
endif;
endforeach;
return $form;
}
add_filter('gform_pre_render', 'env_populate_country');
Please help...