Drop-down field not saving - dynamically populated

Hi, I see a couple of answers to the same issue here but I’m not having much luck - I hope someone would kindly have a quick look over my code and see if they can spot anything untoward… It’s driving me mental!

Many thanks in advance

function prepopulate_all_names($form, $formID, $fieldID, $array){
	if($form["id"] != $formID) {
       return $form;
	}

	$args = array(
		'order'		 => 'ASC',
		'role__in' => array('subscriber','staff_admin')
	);
	$wp_user_query = new WP_User_Query($args);
	$users = $wp_user_query->get_results();

    //Creating drop down item array.
    $items = array();
    $items[] = array( 'text' => 'Select a user...', 'value' => '0' );
	
	//Add any additional items
	if ($array) {
		$items[] = $array;
	}

    foreach ( $users as $userLoop ) { 
		$user =  get_userdata($userLoop->ID);
		$name = $user->first_name.' '.$user->last_name;
        $items[] = array( 'text' => $name, 'value' => $name );
    }
    foreach($form["fields"] as &$field) {
        if($field["id"] == $fieldID){
            $field["choices"] = $items;
        }
	}
	return $form;
}

Can you please share all your code including the add_filter lines? Thank you.

Hi Chris, Thanks mate, please see the rest below:

//Add a client user 
add_filter("gform_pre_render_84", "pre_render_84");
function pre_render_84($form){
	$array = '';
	prepopulate_all_names($form, 84, 49, $array);
    return $form;
}

In addition to gform_pre_render, you should be using three other filters as shown here (the first 4 lines in The Code section here):

Your lines would look like this:

add_filter( 'gform_pre_render_84', 'pre_render_84' );
add_filter( 'gform_pre_validation_84', 'pre_render_84' );
add_filter( 'gform_pre_submission_filter_84', 'pre_render_84' );
add_filter( 'gform_admin_pre_render_84', 'pre_render_84' );

There is an alternate method as well, which uses one filter rather than 4, which also works (it’s a newer and more efficient method than the 4 filters were previously):

Be sure you use either this last filter, or all four of the others, and then test your code and dynamic population again. Let us know how that turns out.

I would also use object notation to interact with the fields. So this:

if($field["id"] == $fieldID){
    $field["choices"] = $items;
}

would change to this:

if ( $field->id == $fieldID ) {
    $field->choices = $items;
}
1 Like

Hi All,
Thanks for your suggestions and advice.

Dynamically populated drop-downs are populating fine but still not saving to the db after trying all your suggestions. Annoyingly the drop-downs are behaving as expected when using Gform ‘Preview’ option which makes me think it’s a theme/plugin issue.

I’ll nibble away at it and if I fine an obvious answer I’ll pop it on here for future ref.

Cheers, M

A little bit more for you knowledgable gents in case you have stumbled on this before!

On my build dynamically populated drop-downs are not saving when the form is embedded on a custom post type. They are saving to the db fine on posts and pages. I’m using the same template code for posts and my CPT whilst testing this. I’ve tried the following:

  • With/without ‘show_in_rest’ when declaring CPT
  • Turning off all plugins
  • Switching to 2019 theme (I’m using WP Bootstrap Starter and child theme)

Please can you have a quick squint at my CPT code and let me know if I’ve missed anything out or if you have any other ideas to what I can do to test this further please?

function forms_cpt() {
    $labels = array(
         'name' => 'Form Pages',
        'singular_name' => 'Form Page',
        'add_new' => 'Add New Form Page',
        'add_new_item' => 'Add New Form',
        'edit_item' => 'Edit Form Page',
        'new_item' => 'New Form Page',
        'all_items' => 'All Forms',
        'view_item' => 'View Form Pages',
        'search_items' => 'Search Form Pages',
        'not_found' =>  'No Form Pages Found',
        'not_found_in_trash' => 'No Form Pages found in Trash', 
        'parent_item_colon' => '',
        'menu_name' => 'Form Page',
    );

    register_post_type( 'form-page', array(
        'labels' => $labels,
        'has_archive' => false,
        'public' => true,
        'show_in_nav_menus' => false,
        'show_in_menu'        => true,
        'exclude_from_search' => false,
        'capability_type' => 'post',
        'rewrite' => array( 'slug' => 'forms-restricted' ),
        'menu_icon' => 'dashicons-groups',
        'supports' => array('title','editor')
        )
    );
}
add_action( 'init', 'forms_cpt' );

Again, many thanks in advance for any thoughts or ideas I can try :slight_smile: