可重复字段组中的可重复字段

时间:2014-07-03 作者:davidcondrey

复合开Saving metabox repeatable fields ...

我创建了一个元盒,其中包含一系列可重复的字段。然而,在这些字段中,我想创建另一个名为Choices的字段,并使其在每个分组中都可以重复。

我可以设置附加字段并重复它,但不知道如何保存新的可重复字段的值。我试图创建的新字段位于下面代码的注释部分以及我保存它的尝试中

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 );
}

1 个回复
SO网友:cfx

使用Advanced Custom Fields. 当我可以自己做一些事情时,我鄙视使用插件,但ACF对于解决这样的代码噩梦已经变得不可或缺了。

结束