Support

Account

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

Solved

Dynamic Select from Options Textarea – Not saving selected value on post edit

  • Hi there!
    I followed the steps outlined on (http://www.advancedcustomfields.com/resources/tutorials/dynamically-populate-a-select-fields-choices/).

    I got my select field to populate the lined info from the textarea on an options page for a CPT no problem. However, when I update/publish the post of the CPT, the selected option displays the the 1st option instead of what was selected prior to updating the page. Further more, lets say I have three values set. If value 3 is selected, which is the last one from the textarea option, that one will remain after updating the post. The revert happens on the values after the first one and before the last one.

    The value looks to be correct via get_field…. if I echo the saved option on the frontend, but on the actual post edit page, it’s showing the 1st option on the select field. The problem would be if the person re-saves that post, that option would be switched to the first option instead of where it was previously.

    Furthermore, If I go to edit the select field from the ACF area, the choices look like this which would be populated from the textarea:

    Pool1
    : Pool1
    Pool2
    : Pool2
    Pool3 : Pool3

    Instead of:

    Pool1 : Pool1
    Pool2 : Pool2
    Pool3 : Pool3

    I’ve also tested not using the options textarea and manually entered the correct value : label on the select field in ACF and it worked as expected.

    Any help would greatly be appreciated. Thanks!

  • Hi @Bluenotes

    can you please provide the code which you are using?
    Also, some screenshots of the options page value, and the select field in question?

    Thanks
    E

  • Hi elliot,

    Here is the code for the option:

    if( function_exists('acf_add_options_sub_page') ) {
        acf_add_options_sub_page(array(
            'title' => 'Settings',
            'parent' => 'edit.php?post_type=bne_team_schedule',
            'capability' => 'edit_posts'
        ));
    }
    
    function bne_team_schedule_team_names_load_field( $field ) {
    	$field['choices'] = array();
     
    	$choices = get_field('bne_team_schedule_team_names_options', 'option');
     
    	$choices = explode("\n", $choices);
     
    	if( is_array($choices) ) {
    		foreach( $choices as $choice ) {
    			$field['choices'][$choice] = $choice;
    		}
    	}
     
        // Important: return the field
        return $field;
    }
    add_filter('acf/load_field/name=bne_team_schedule_home_team_name', 'bne_team_schedule_team_names_load_field');
    

    Attached is screenshot of the option field which is subpage of the CPT and a screen of what is outputted into the select field within ACF. Like I said, the database seems to store the correct choice but when you update the post the select field, visually, resets to the first choice in the list.

    Using ACF v4.3.0 and Options v1.2.0

  • Hi @Bluenotes

    Thanks for the info. Looks like something must be going wrong with ACF.

    I’ll take a look this weekend and figure it out.

    Thanks
    E

  • 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

  • Thanks Elliot! Your fix worked.

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Dynamic Select from Options Textarea – Not saving selected value on post edit’ is closed to new replies.