• My plugins options page has repeating fields for one of the settings. I have the repeating part working but I cannot get the fields to save.

    public function add_mimes_callback() {
      $option       = $this->ordered_uploads_options;
      $mimes        = $option[OU_PL_PREFIX . '_mimes'];
      $mime_folders = $this->mime_folders();
      ?>
      <script type="text/javascript">
        jQuery(document).ready(function($){
          $('#add-row').on('click', function() {
            var row = $('.empty-row.screen-reader-text').clone(true);
            row.removeClass('empty-row screen-reader-text');
            row.insertBefore('#repeatable-fieldset-one tbody>tr:last');
            jQuery('input, select', row).val('').attr('name', function(index, name) {
              return name.replace(/(\d+)/, function(fullMatch, n) {
                return Number(n) + 1;
              });
            });
            return false;
          });
          $('.remove-row').on('click', function() {
            $(this).parent().parent().remove();
            return false;
          });
        });
      </script>
      <table id="repeatable-fieldset-one" width="100%">
        <thead>
          <tr>
            <th>Extention</th>
            <th>Filetype</th>
            <th>Folder</th>
            <th></th>
          </tr>
        </thead>
        <tbody>
        <?php
          $counter = 1;
          if( $mimes ) {
            foreach( $mimes as $mime ) {
              $counter++;
              $extention = $option[$mimes]['ou_mime_extention_' . $counter];
              $filetype  = $option[$mimes]['ou_mime_filetype_' . $counter];
              $folder    = $option[$mimes]['ou_mime_folder_' . $counter];
              ?>
              <tr>
                <td>
                  <input type="text" name="ou_options_settings['ou_mimes']['ou_mime_extention_<?php echo $counter; ?>']" value="<?php echo $extention; ?>" class="all-options">
                </td>
                <td>
                  <input type="text" name="ou_options_settings['ou_mimes']['ou_mime_filetype_<?php echo $counter; ?>']" class="all-options">
                </td>
                <td>
                  <select name="ou_options_settings['ou_mimes']['ou_mime_folder_<?php echo $counter; ?>']">
                    <?php foreach ( $mime_folders as $label => $value ) : ?>
                      <option value="<?php echo $value; ?>"<?php selected( $field['select'], $value ); ?>><?php echo $label; ?></option>
                    <?php endforeach; ?>
                  </select>
                </td>
                <td>
                  <a class="button remove-row" href="#">Remove</a>
                </td>
              </tr>
              <?php
            }
          } else {
            ?>
            <tr>
              <td>
                <input type="text" name="ou_options_settings['ou_mimes']['ou_mime_extention_1']" class="all-options">
              </td>
              <td>
                <input type="text" name="ou_options_settings['ou_mimes']['ou_mime_filetype_1']" class="all-options">
              </td>
              <td>
                <select name="ou_options_settings['ou_mimes']['ou_mime_folder_1']">
                  <?php foreach ( $mime_folders as $label => $value ) : ?>
                    <option value="<?php echo $value; ?>"><?php echo $label; ?></option>
                  <?php endforeach; ?>
                </select>
              </td>
              <td>
                <a class="button remove-row" href="#">Remove</a>
              </td>
            </tr>
            <?php
          }
          ?>
          <tr class="empty-row screen-reader-text">
            <td>
              <input type="text" name="ou_options_settings['ou_mimes']['ou_mime_extention_1']" class="all-options">
            </td>
            <td>
              <input type="text" name="ou_options_settings['ou_mimes']['ou_mime_filetype_1']" class="all-options">
            </td>
            <td>
              <select name="ou_options_settings['ou_mimes']['ou_mime_folder_1']">
                <?php foreach ( $mime_folders as $label => $value ) : ?>
                  <option value="<?php echo $value; ?>"><?php echo $label; ?></option>
                <?php endforeach; ?>
              </select>
            </td>
            <td>
              <a class="button remove-row" href="#">Remove</a>
            </td>
          </tr>
          <?php
        ?>
        </tbody>
      </table>
      <p><a id="add-row" class="button" href="#">Add Mime</a></p>
      <?php
    } // end mime_repeater

    Here’s a link to the full code Gist

    I’ve been trying to get this to work for days. Unfortunately, I can’t find very much on repeatable fields, so If any one can help me out I’d really appreciate it.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    I really doubt you can use the Settings API in this manner. All the fields need to be defined by PHP before the form is sent out. Any changes to the form by jQuery invalidate the API. AFAIK anyway, I could be wrong.

    I think you need to develop your own page template that presents the form and processes the submit action. As a page template, the WP environment is properly initialized. To reach the page from the back end add a menu or sub-menu page that simply redirects to the page you’ve created. The menu callback essentially contains echo "<script>window.location.assign('$path');</script>";

    Thread Starter wpgaijin

    (@wpgaijin)

    It’s not a page template, it a plugin options page in the admin.

    Well, there are plenty of plugins and frameworks that use repeatable fields for settings. I don’t see why I couldn’t save nested serialized data via the Settings API.

    Moderator bcworkz

    (@bcworkz)

    Yes, I know, I was suggesting a work around. Properly styled, other than a brief interstitial page, it will appear just as an options page.

    If you know of plugins that use repeatable fields via the Options API you should use them as examples of the proper approach.

    Hi wpgaijin,

    Did you get the solution? I also need the same thing to happen.

    Thanks.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Saving repeating fileds’ is closed to new replies.