Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Compounding on Saving metabox repeatable fields ...

I've created a metabox with a series of fields that are repeatable. However, within those fields, I'd like to create another field named Choices and make that repeatable within each grouping.

I was able to setup the additional field and repeat it, but have no idea how to save the value of the new repeatable field. The new field that I was trying to create is within the commented portions of the code below as well as my attempts at saving it

function poll_potential_results() {
    $options = array (
        'Option 1' => 'option1',
        'Option 2' => 'option2',
    );
    return $options;
}

add_action('admin_init', 'poll_add_metaboxes', 1);
function poll_add_metaboxes() {
    add_meta_box( 'Dicey', 'Questions and Options', 'poll_metabox', 'quizzes', 'normal', 'default');
}

function poll_metabox() {
    global $post;
    $repeat_fields = get_post_meta($post->ID, 'repeat_fields', true);
    $options = poll_potential_results();
    wp_nonce_field( 'poll_metabox_nonce', 'poll_metabox_nonce' );
    ?>
    <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.insertAfter( '#repeatable_row' );
            return false;
        });
        $( '.remove-row' ).on('click', function() {
            $(this).parents('tr').remove();
            return false;
        });
        /*$('#add-choice' ).on('click',function() {
            var choicenew = $(this).closest('tr' ).clone(true);
            var choice = $(this ).closest('tr');
            choicenew.insertAfter(choice);
            return false;
        })*/
    });
    </script>
    <table id="repeatable_row" width="100%"><tbody>

    <?php if ( $repeat_fields ) : foreach ( $repeat_fields as $field ) { ?>
    <tr><td><table>
        <tr>
            <td colspan="3"><input type="text" class="widefat" name="name[]" value="<?php if($field['name'] != '') echo esc_attr( $field['name'] ); ?>" /></td>
        </tr>
        <tr>
            <!--<td><a id="add-choice" class="button" href="#">Add Choice</a></td>-->

            <!--<td><input type="text" class="widefat" name="choice[]" value="<?php //if($field['choice'] != '') echo esc_attr( $field['choice'] ); ?>" /></td>-->
            <td><input type="text" class="widefat" name="url[]" value="<?php if ($field['url'] != '') echo esc_attr( $field['url'] ); else echo 'http://'; ?>" /></td>
            <td>
                <select name="select[]">
                <?php foreach ( $options 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>
    </table></td></tr>

    <?php } else : ?>
    <tr><td><table>
        <tr>
            <td colspan="3"><input type="text" class="widefat" name="name[]" /></td>
        </tr>
        <tr>
            <!--<td><a id="add-choice" class="button" href="#">Add Choice</a></td>-->

            <!--<td><input type="text" class="widefat" name="choice[]" /></td>-->
            <td><input type="text" class="widefat" name="url[]" value="http://" /></td>
            <td>
                <select name="select[]">
                <?php foreach ( $options 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>
    </table></td></tr>

    <?php endif; ?>

    <!-- empty hidden one for jQuery -->
    <tr class="empty-row screen-reader-text"><td><table>
        <tr>
            <td colspan="3"><input type="text" class="widefat" name="name[]" /></td>
        </tr>
        <tr>
            <!--<td><a id="add-choice" class="button" href="#">Add Choice</a></td>-->
            <!--<td><input type="text" class="widefat" name="choice[]" /></td>-->
            <td><input type="text" class="widefat" name="url[]" value="http://" /></td>
            <td>
                <select name="select[]">
                <?php foreach ( $options 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>
    </table></td></tr>
    </tbody>
    </table>
    <p><a id="add-row" class="button" href="#">Add another</a></p>
<?php }

add_action('save_post', 'poll_metabox_save');
function poll_metabox_save($post_id) {
    if ( ! isset( $_POST['poll_metabox_nonce'] ) || ! wp_verify_nonce( $_POST['poll_metabox_nonce'], 'poll_metabox_nonce' ) ) return;
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
    if (!current_user_can('edit_post', $post_id)) return;

    $old = get_post_meta($post_id, 'repeat_fields', true);
    $new = array();
    //$newchoice = $new[$j];
    //$newchoice = $new;
    //

    $options = poll_potential_results();
    $names = $_POST['name'];
    //$choices = $_POST['choice'];
    $selects = $_POST['select'];
    $urls = $_POST['url'];

    $count = count( $names );
    //$countchoices = count($choices);

    for ( $i = 0; $i < $count; $i++ ) {
        if ( $names[$i] != '' ) {
            $new[$i]['name'] = stripslashes( strip_tags( $names[$i] ) );

            //for ( $j = 0; $j < $countchoices; $j++) {
            //  if ( $choices[$j] != '') {
            //      $newchoice[$j]['choice'] = stripslashes(strip_tags($choices[$j]));
            //  }
            //}

            if ( in_array( $selects[$i], $options ) ) $new[$i]['select'] = $selects[$i];
            else $new[$i]['select'] = '';
            if ( $urls[$i] == 'http://' ) $new[$i]['url'] = '';
            else $new[$i]['url'] = stripslashes( $urls[$i] );
        }
    }
    //if (!empty($newchoice)) {
    //  update_post_meta($post_id,'repeat_fields',$newchoice);
    //}

    if ( !empty( $new ) && $new != $old ) update_post_meta( $post_id, 'repeat_fields', $new );
    elseif ( empty($new) && $old ) delete_post_meta( $post_id, 'repeat_fields', $old );
}
share|improve this question

1 Answer 1

Use Advanced Custom Fields. I despise using plugins when I can do something myself but ACF has become indispensable for solving code nightmares like this.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.