Support

Account

Home Forums Add-ons Options Page Problem populating select field choices

Solved

Problem populating select field choices

  • Im using this method to populate select field choices, but im unable to get it to work. Drop down list is always empty no matter what i do. Is there something i need to know about this?

    test1 = empty selection field
    test2 = textarea

    If i save the page with even 1 line in test2 textarea it doesnt show up in the selection list/dropdown in test1 field.

    function acf_load_test1_field_choices( $field ) {
        
        // reset choices
        $field['choices'] = array();
            
        // get the textarea value from options page without any formatting
        $choices = get_field('test2', 'option', false);
     
       // explode the value so that each line is a new array piece
        $choices = explode("\n", $choices);
        
        // loop through array and add to field 'choices'
        if( is_array($choices) ) {
            
            foreach( $choices as $choice ) {
                
                $field['choices'][ $choice ] = $choice;
                
            }
            
        }
        
        // return the field
        return $field;    
    }
    add_filter('acf/load_field/name=test1', 'acf_load_test1_field_choices');

    Wordpress 4.2.2
    Acf 4.4.1

  • What you have looks like it could work. What will help is to see what’s in those values. Could you alter your function and let me know what’s output when you edit the post.

    
    function acf_load_test1_field_choices( $field ) {
        
        // reset choices
        $field['choices'] = array();
            
        // get the textarea value from options page without any formatting
        $choices = get_field('test2', 'option', false);
        
       // see what's in choices
       echo '<pre>',htmlentities($choices),'</pre>';
     
       // explode the value so that each line is a new array piece
        $choices = explode("\n", $choices);
    
       // see what's in choices
       echo '<pre>'; print_r($choices); echo '</pre>';
        
        // loop through array and add to field 'choices'
        if( is_array($choices) ) {
            
            foreach( $choices as $choice ) {
                
                $field['choices'][ $choice ] = $choice;
                
            }
            
        }
        
        // see what's in choices
        echo '<pre>'; print_r($field['choices']);
    
        // return the field
        return $field;    
    }
    add_filter('acf/load_field/name=test1', 'acf_load_test1_field_choices');
    
  • This is the output:

    Array
    (
        [0] => 
    )
    
    Array
    (
        [] => 
    )
    
    test1

    I tried to delete the acf form and re-created it using different names but still it doesnt catch any of the values.

    Do i have something wrong with these lines:
    function my_acf_load_test1_field_choices( $field ) {
    or
    add_filter('acf/load_field/name=test1', 'acf_load_test1_field_choices');

  • The code looks okay, I have some questions about splitting on “n”, but we can’t check that until there is something to split. It appears that $choices = get_field('test2', 'option', false); is not getting anything, so you need start there.

    Check that field name and that there is a value in it on the options page. Until you see the value that is in that field echoed by the following you can’t get any choices.

    
    $choices = get_field('test2', 'option', false);
       // see what's in choices
       echo '<pre>',htmlentities($choices),'</pre>';
    
  • It now works with this line:
    $choices = get_field('test2');

    I might have some other problems later on as i need to change this quite a bit. Thanks for the help!

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

The topic ‘Problem populating select field choices’ is closed to new replies.