使用下面的示例,我可以成功地在 Gravityforms 编辑器中填充一个下拉列表,其中包含存储在我的数据库表中的状态列表......
add_filter("gform_pre_render_3", populate_dropdown1); //5 is the GF Form ID
add_filter("gform_admin_pre_render_3", populate_dropdown1);
function populate_dropdown1($form){
global $wpdb; //Accessing WP Database (non-WP Table) use code below.
$results = $wpdb->get_results("SELECT btc_state_short from btc_state_list");
$choices = array();
$choices[] = array("text" => "Select a State", "value" => ""); //adding a array option with no value, this will make the user select and option.
foreach ($results as $result) {
$choices[] = array("text" => $result->btc_state_short, "value" => $result->btc_state_short);
}
foreach($form["fields"] as &$field){
if($field["id"] == 1){
$field["choices"] = $choices;
}
}
return $form;
}
这很好用,但只有在您单击“更新表单”后。当您最初创建下拉列表时,它具有标准字段...
First Choice
Second Choice
Third Choice
有没有办法在拖入后立即预先填充它?