Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Gravityforms - Programmatically add a drop down WordPress

  • SOLVED

I am using GravityForms and am trying to create some pre-set Fields, I am ok with the standard text ones but I can't seem to work out how to create a custom drop-down with my own pre-defined choices inside.

I am trying to do this programmatically using gform_add_field_buttons

Can somebody give me a simple example?

I am messing about with this....

add_filter("gform_pre_render_5", populate_dropdown); //5 is the GF Form ID
add_filter("gform_admin_pre_render_5", populate_dropdown);

function populate_dropdown($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 value", "value" => "");
$choices[] = array("text" => "1", "value" => "1");
$choices[] = array("text" => "2", "value" => "2");
$choices[] = array("text" => "3", "value" => "3");
$choices[] = array("text" => "4", "value" => "4");



foreach($form["fields"] as &$field){
if($field["id"] == 1){
$field["choices"] = $choices;
}
}

return $form;
}



This works and displays a list of pre-defined values but only after I click 'Submit Form' and the form is refreshed. Is there a way to pre-define these fields so that they display as soon as I add the field to the form?

Answers (3)

2014-01-20

Arnav Joy answers:

can you show your form please.


macster448 comments:

Hi Arnav,

How do you mean show my form? Do you want a screenshot?

2014-01-20

Ryan S answers:

<?php

Do you intentionally remove quote in these line?


add_filter("gform_pre_render_5", "populate_dropdown");
add_filter("gform_admin_pre_render_5", "populate_dropdown");



Try this

add_filter('gform_pre_render_5', 'populate_dropdown');
function populate_dropdown() {
global $wpdb;

foreach( $form['fields'] as &$field ) {
// update "populate-dropdown" to dropdown(select) CSS class
if($field['type'] != 'select' || strpos($field['cssClass'], 'populate-dropdown') === false)
continue;

// our dropdown options
$choices = array(array('text' => 'Select a Post', 'value' => ' '));

// select our custom fields
$posts = $wpdb->get_results( "SELECT ID, btc_state_short FROM btc_state_list" );
foreach( $posts as $post ) {
$choices[] = array('text' => $post->ID, 'value' => $post->btc_state_short);
}

$field['choices'] = $choices;

// if this do nothing, try
// return $field (uncomment if neeed)
}
}



You can follow tutorial in here http://www.gravityhelp.com/documentation/page/Dynamically_Populating_Drop_Down_Fields


Hope that helps


macster448 comments:

Hik Ryan,

I am confused by your solution. You have taken out out my predefined choices and this no longer works.

If you read my original question you will see this line...

<strong>This works and displays a list of pre-defined values but only after I click 'Submit Form' and the form is refreshed. Is there a way to pre-define these fields so that they display as soon as I add the field to the form?</strong>

This it he problem I am experiencing


Ryan S comments:

Try adding priority, like what @bhavesh suggested, e.g.


add_filter('gform_pre_render_5', 'populate_dropdown', 99);

2014-01-20

Bob answers:

can you post link here so we can see the form?


Bob comments:

Your code seems perfect do you added any other filter on the same form?


Bob comments:

Try changing priority of your filters
first try lower values like 2

add_filter("gform_pre_render_5", populate_dropdown,2); //5 is the GF Form ID
add_filter("gform_admin_pre_render_5", populate_dropdown,2);

if still not work then try setting higher valuer like 20