Support

Account

Home Forums General Issues Interface support for optgroup?

Solved

Interface support for optgroup?

  • Hi there,

    I’ve read around this issue in the support forms, especially this topic – http://support.advancedcustomfields.com/forums/topic/how-do-i-get-optgroups-in-the-select-field/ (and an old topic here here)

    Could someone please explain this to me?

    Hi @YPGabel,

    Good news! I’ve just update the select field to automatically determine the optgroup setting!

    You can update to the latest code on github to start using this feature!

    Cheers Elliot

    How does the select field automatically determine it? I can’t see any option in the interface to use optgroup.

    I really like the idea of @alfonsosiloniz’s plugin/hack, which alters ACF so that in the interface so that you can enter optgroups like so, with a dash preceding the optgroup:

    “-Europe
    Spain
    France
    -America
    Argentina
    Peru”

    However I am working on a site which is used by clients with hardly any computer expertise. I need the select field in use in the backend, on a custom post type, to be enabled for OPTGROUP. I would suggest adding a checkbox option in the interface for optgroups, they are a very underused but important feature of forms and it would be great if ACF supported them fully. Failing that, could someone please advise me on the current best practice for implementing them?

    I think what I need to do is what Elliot suggests here but I don’t know how to do this, and whether this would make the optgroup visible in the admin section, when you enter the choice in a custom post type.

    Thank you.

    Patrick

    EDIT: By the way I know this is my first post but I am developer working for a paying user of ACF username: [email protected] Cheers!

  • Hi @patrickzdb

    Thanks for the question. Currently, optgroup functionality is not supported into the UI textarea when editing a field.

    This said, you can use a filter called acf/load_field to modify the select field’s choices. This is detailed in the tutorial found here:
    http://www.advancedcustomfields.com/resources/tutorials/dynamically-populate-a-select-fields-choices/

    I will add this feature request to my to-do list and you can expect to see it soon.

    But for now, you will need to use this filter to modify the $field[‘choices’]

    A normal choices array looks like this:

    
    <?php 
    
    $field['choices'] = array(
    	'red' => 'Red',
    	'blue' => 'Blue'
    );
    
     ?>
    

    And an optgroup choices looks like this:

    
    <?php 
    
    $field['choices'] = array(
    	'Colors' => array(
    		'red' => 'Red',
    		'blue' => 'Blue'
    	),
    	'Shades' => array(
    		'black' => 'Black',
    		'white' => 'White'
    	),
    );
    
     ?>
    

    Hope that helps.

    Thanks
    E

  • Thanks a lot for your feedback Elliot. I think I’ll just wait until ACF has support for OPTGROUP in the backend, as the client needs to be able to choose it.

    I have one other feature I would like to request or see is possible. When adding a new custom field, with a type of select I would like the choices to be populated from all of the entries in an existing custom post type, instead of manually inputting loads into the Choices textarea. So when creating a new post, the user can select from a dropdown list of all posts in say “Venues” custom post type. I was wondering if you could point me in the right direction of how to do this, and if you think it’s a good suggestion feel free to add it to your todo list.

    Cheers!

  • Aha! Nevermind, I figured it out.

    And unlike some people I’ll actually put how I did it! It annoys me so much when people don’t.

    Basically you have to select Field Type: Page Link, then you get the option to choose an existing custom post type in a row which appears below. Great feature, cheers.

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

The topic ‘Interface support for optgroup?’ is closed to new replies.