Support

Account

Home Forums ACF PRO ACF Custom Field Type

Solved

ACF Custom Field Type

  • I have created a custom field type using elliots CFT template: –

    https://github.com/AdvancedCustomFields/acf-field-type-template

    Now I’d like to extend my CFT by letting my CFT render two input fields and save the two fields in an array.

    However when I try this my CFT only lets me store one value but if I converted all my inputs to store arrays it simply wipes the other when saved so only one array can be saved.

    Am I missing something ?

  • What does your render field method look like?
    Given the example in the field type template it should probably look something like

    
    ?>
      <input type="text" name="<?php echo esc_attr($field['name']) ?>[field1]" value="<?php echo esc_attr($field['value']['field1']) ?>" style="font-size:<?php echo $field['font_size'] ?>px;" />
      <input type="text" name="<?php echo esc_attr($field['name']) ?>[field2]" value="<?php echo esc_attr($field['value']['field2']) ?>" style="font-size:<?php echo $field['font_size'] ?>px;" />
    <?php
    

    You should notice that I turned the value into a nested array and I’m also echoing out each of the values of this array independently.
    I’m not 100% sure that this is all you’d need to do. You may need to also do some work on the save_field and load_field methods and you’ll certainly want to do some work on the format_value method.

  • Stripped back it looks like this :-

    
    <input type="text" name="<?php echo esc_attr($field['name']) ?>image" id="image_url"
                           class="regular-text"
                           value="<?php echo esc_attr($field['value']['image']) ?>">
    
    echo '<input type="text" id="pin_json"  name="' . esc_attr($field['name']) . 'json' . '"  value="' . esc_attr($field['value']['json']) . '"  /> ';
    
    

    Also modified the value to store an array that looks like this.

    
       [value] => Array
            (
                [image] => 
                [json] => 
            )
    

    But it still wont save my values 🙁 !

  • Your additions to the field names need to have brackets around them

    
      <input type="text" name="<?php echo esc_attr($field['name']) ?>[image]" id="image_url"
                           class="regular-text"
                           value="<?php echo esc_attr($field['value']['image']) ?>">
    
    echo '<input type="text" id="pin_json"  name="' . esc_attr($field['name']) . '[json]' . '"  value="' . esc_attr($field['value']['json']) . '"  /> ';
    

    I’m not sure what the results of not having them would be, more than likely your field name in the form looks something like FIELDNAMEimage which is a completely different name than you expect it to be.

  • Hi John,

    Thanks for this managed to get everything sorted and is now saving all values!

    Thanks for all your help!

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

The topic ‘ACF Custom Field Type’ is closed to new replies.