Support

Account

Home Forums General Issues Need help with creating new field type with ACF

Solved

Need help with creating new field type with ACF

  • Hello!
    I started to use ACF a few days ago. I want to create a new field type. I followed the instructions of the official site. But i don’t understand something.
    I want my field contain two text-area.
    I don’t really know how to do it. So i created a new field, it’s appearing in the edit-page, but i can’t handle the two values. Here is how my create_field function looks like in the php-file of my field (i think that’s what i need to change):

    function create_field( $field ){ ?>
        <table>
          <tr> <th scope="col">Year</th> <th scope="col">Text</th> </tr>
          <tr>
             <td>
              <?php echo '<textarea id="' . $field['id'] . 'year"
                  class="' . $field['class'] . '"
                  name="' . $field['name'] . 'year" >'
                  . $field['value']['year'] .
                '</textarea>';
              ?>
            </td>
            <td>
              <?php echo '<textarea id="' . $field['id'] . 'text"
                class="' . $field['class'] . '"
                name="' . $field['name'] . 'text" >'
                  . $field['value']['text'] .
                '</textarea>';
              ?>
            </td>
          </tr>
        </table>
        <?php
    }

    Now, ACF always save the value of the second text-area. What i don’t understand, is how ACF know to save that? And how can i save the value of the first too?

    I’m also interested in tutorials if you can suggest me anything in this field.
    I tried to find more field-types in the internet (more than the built in ones), but i didn’t found anything.

    Thanks for your help:
    Koli

  • Hi @koli14

    Have you looked at the HTML source that this PHP produces? I believe your textarea will have a name like this:

    
    fields[field_key]text
    

    but what you want is:

    
    fields[field_key][text]
    

    Adding the extra square brackets will allow the data to be correctly posted

  • Yes, i checked the generated HTML source, and played with a few versions (with brackets, in quotes…), i’m not expert in PHP…
    It still not working, maybe i asked the question wrong.
    So the name of the textarea is how ACF knows which field to save (not the ID or class)? So if i want two textarea, they need to have different names.
    Now the working field has a name: $field[‘name’] which generates: fields[field_51fbdeb7f1609].
    What should i add as a name to the second textarea, and how can i get the value of it?
    I can get the value of the field with the $field[‘value’], but it’s just the value of the textare with the name: $field[‘name’].
    Uhhh, maybe it’s a bit too complicated for me…

  • Hi @koli14

    Yes, it is the name which is used to post data.

    As mentioned above, if you want to send 2 textarea’s in the same name, you will need to use square brackets to tell the browser it is an array of information.

    For example, you want the output to be:

    
    <textarea name="fields[field_123][textarea_1]"></textarea>
    <textarea name="fields[field_123][textarea_2]"></textarea>
    
  • I tried already what you write, and then the HTML result is:

    <textarea id="acf-field-text-year" class="year_n_text" name="f"></textarea>
    <textarea id="acf-field-vita-year" class="year_n_text" name="f"></textarea>
  • Hi @koli14

    The output does not look correct.
    Please attach the code you are using in the create_field function

  • I’m sure that a lot of thing missing from my field to work, but i have no idea what.
    Here is my hole field:

    <?php
    class acf_field_year_n_text extends acf_field{
      var $settings, 
        $defaults;
      function __construct(){
        $this->name = 'year_n_text';
        $this->label = __('Year and text');
        $this->category = __("Content",'acf');
        $this->defaults = array(
          'default_value'  =>  '',
          'formatting'   =>  'br',
        );
        parent::__construct();
        $this->settings = array(
          'path' => apply_filters('acf/helpers/get_path', __FILE__),
          'dir' => apply_filters('acf/helpers/get_dir', __FILE__),
          'version' => '1.0.0'
        );
      }
      function create_options($field){
        $key = $field['name'];
        ?>
        <tr class="field_option field_option_<?php echo $this->name; ?>">
          <td class="label">
            <label><?php _e("Formatting",'acf'); ?></label>
            <p class="description"><?php _e("Define how to render html tags",'acf'); ?></p>
          </td>
          <td>
            <?php 
            do_action('acf/create_field', array(
              'type'  =>  'select',
              'name'  =>  'fields['.$key.'][formatting]',
              'name2'  =>  'fields['.$key.'2][formatting]',
              'value'  =>  $field['formatting'],
              'choices' => array(
                'none'  =>  __("None",'acf'),
                'br'  =>  __("auto <br />",'acf'),
                'html'  =>  __("HTML",'acf'),
              )
            ));
            ?>
          </td>
        </tr>
        <?php
      }
      function create_field( $field ){
        ?>
        <table >
          <tr>
            <th scope="col">Year</th>
            <th scope="col">Text</th>
          </tr>
          <tr>
            <?php var_dump ($field['value']); ?>
            <td>
              <?php echo '<textarea 
                id="' . $field['id'] . '" 
                rows="4" 
                class="' . $field['class'] . '"
                name="' . $field['name']['textarea_1'] . '" >'
                  . $field['value']['textarea_1'] . 
                '</textarea>';
              ?>
            </td>
            <td>
              <?php echo '<textarea 
                id="' . $field['id'] . '" 
                rows="4" 
                class="' . $field['class'] . '"
                name="' . $field['name']['textarea_2'] . '" >'
                  . $field['value']['textarea_2'] . 
                '</textarea>';
              ?>
            </td>
          </tr>
        </table>
        <?php
      }
      function input_admin_enqueue_scripts(){
        wp_register_script('acf-input-year_n_text', $this->settings['dir'] . 'js/input.js', array('acf-input'), $this->settings['version']);
        wp_register_style('acf-input-year_n_text', $this->settings['dir'] . 'css/input.css', array('acf-input'), $this->settings['version']);
        wp_enqueue_script(array(
          'acf-input-year_n_text',
        ));
        wp_enqueue_style(array(
          'acf-input-year_n_text',
        ));
      }
    }
    new acf_field_year_n_text();
    ?>
  • This is incorrect:

    
     name="' . $field['name']['textarea_2'] . '" >'
    

    It should be:

    
     name="' . $field['name'] . '[textarea_2]" >'
    

    There is a major different between the 2

  • Thanks, yes, now i understand why!

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

The topic ‘Need help with creating new field type with ACF’ is closed to new replies.