• Resolved codenamebrian

    (@codenamebrian)


    Hi! Love the plugin, really helps extend ACF! My question is about the Allow ‘custom’ values to be added in select field option. From what I can tell its only saving one value to that field. Is there a syntax to ensure it saves both a value and the text such as:
    <option value=”1234″>Save This Text</option>

    So in that scenario I would like to save “1234” & “Save This Text” to the field. I tried using the ACF syntax “Save This Text : 1234” with no luck.

    Is this possible with the ACF Extended plugin?

    Thanks!

Viewing 1 replies (of 1 total)
  • Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Thanks for the feedback! I’m glad you enjoy ACF Extended ??

    Unfortunately, there is no way to save both value & display text at the same time using the “Allow custom value” settings in the Select field. There’s no “Global pattern” that could match every needs. Some users may would like to save the custom value Remember this : you are amazing for example, which would end up splitting the value to Remember this.

    You can add your own pattern tho, using the acf/update_value hook. See documentation: https://www.advancedcustomfields.com/resources/acf-update_value/

    Here is an example usage for your case, inspired by the ACF Checkbox Field which let user save it’s own custom value in the field settings. This code will split the custom value with the pattern value : Text:

    add_filter('acf/update_value/name=my_select', 'my_acf_select_split_custom_value', 10, 3);
    function my_acf_select_split_custom_value($value, $post_id, $field){
        
        // Force value to array
        $_value = acf_array($value);
        
        // Vars
        $values = array();
        $choices = $field['choices'];
        
        // Updater
        $update = false;
        
        // Construct Choices
        foreach($_value as $v){
        
            // Explode value
            $_v = explode(' : ', $v);
            
            $values[] = $_v[0];
            
            // value : text wasn't found
            if(!isset($_v[1]))
                continue;
        
            // ignore if already eixsts
            if(isset($choices[$_v[0]]))
                continue;
            
            $update = true;
        
            // unslash & sanitize (remove tags) value
            $_v[0] = wp_unslash($_v[0]);
            $_v[0] = sanitize_text_field($_v[0]);
        
            // unslash & sanitize (remove tags) text
            $_v[1] = wp_unslash($_v[1]);
            $_v[1] = sanitize_text_field($_v[1]);
        
            // append
            $choices[$_v[0]] = $_v[1];
            
        }
        
        if($update){
        
            // Inspired by Checkbox field
            $selector = $field['ID'] ? $field['ID'] : $field['key'];
            $field = acf_get_field($selector);
        
            // bail early if no ID (JSON won't be updated)
            if($field['ID']){
                
                $field['choices'] = $choices;
                
                // Save field choices
                acf_update_field($field);
                
            }
            
        }
        
        if(is_array($value)){
            
            return $values;
            
        }
        
        return $values[0];
        
    }
    

    This will save custom values into the field settings choices UI, just like Checkbox Field. Please keep in mind that it won’t work with Json Sync, you have to keep your field group in the DB.

    Video demo: https://i.imgur.com/ZP29Ze5.mp4

    I may add a setting in the future with few patterns to pick for that specific case.

    Hope it helps!

    Regards.

Viewing 1 replies (of 1 total)
  • The topic ‘Allow ‘custom’ values to be added in select field’ is closed to new replies.