Support

Account

Home Forums General Issues Dynamic Select from Options Textarea – Not saving selected value on post edit Reply To: Dynamic Select from Options Textarea – Not saving selected value on post edit

  • Hi @Bluenotes

    Thanks again for the bug report.
    I have tracked down the issue and have a solution for you.

    The issue stems from the get_field function. This function is a theme friendly helper function which also formats the returned value. Because the text area has an option to convert any line breaks to the ‘br’ tag.

    So your $choices value actually contains br tags! You can check this with some simple debugging.

    This explains why values won’t save (because the MARKUP is invalid – br tag within select option’s value attribute), and why the field’s choices setting looks odd.

    All you need to do is improve your function like so:

    
    function bne_team_schedule_team_names_load_field( $field ) {
    	$field['choices'] = array();
     
    	$choices = get_field('bne_team_schedule_team_names_options', 'option', false);
     
    	$choices = explode("\n", $choices);
    	$choices = array_map('trim', $choices);
    	
    	if( is_array($choices) ) {
    		foreach( $choices as $choice ) {
    			$field['choices'][$choice] = $choice;
    		}
    	}
     
        // Important: return the field
        return $field;
    }
    
    Please note the 2 changes:
    1. get_field function is using the 3rd param to prevent formating
    2. arrray_map function will trim any white spaces from the value!
    

    Hope that helps.
    Thanks
    E