Support

Account

Home Forums General Issues Select options not working when the choice string include comma

Solving

Select options not working when the choice string include comma

  • Hi,

    I have a select where I have some choices which include a comma. For example:

    Anna
    Doe, John
    Marie
    Keaton, Marc

    When I choose any of the options which include a comma it doesn’t work. For example, when I select ‘Doe, John’ or ‘ Keaton, Marc’ and I save the post, the saved option is Anna (the first one in the list) instead of the selected option with comma.

    How can I fix it?

    Thanks.

  • The value is saved properly into the database, and will be reflected on the front-end correctly, but on the admin side the value won’t be reflected. This is troubling because if you save again, you will overwrite the field if you don’t remember to update it.

    This seems to be happening because the value for select fields is run through the acf_force_type_array() function. That function has this troublesome code:

    
    if( is_string($var) ) {	
        return explode(',', $var);
    }
    

    Basically when you are on the admin side, it splits your Doe, John value into two values, Doe and John. Since that doesn’t match any of the values in the HTML, the select is left on the default first option.

    I tried replacing the commas with , which is the HTML Entity for a comma, but it causes a different issue, that value is reflected as , instead, and so you end up with the same non-matching issue as before.

    I don’t have a fix for this, but thought I’d shed some light on what’s happening. Hopefully Elliot can chime in and take a look at a fix for this.

  • just to not get a fix for select only:
    it happens at other field-types too that get saved as a comma separated array list.

    but i dont know if it is a ACF only problem or a WP problem too

  • Hi @ivgogra

    This is a WP limitation and ACF will tend to split values separated by a comma to two values.

    You can make use of another character such as full stop and then use the function str_replace to get back the comma.

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

The topic ‘Select options not working when the choice string include comma’ is closed to new replies.