Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am working on a meta box for events. Each event has it's own page with a meta box for the line-up, background image, tickets page URL and some text.

To create the line-up I would like to use the repeatable fields from the "Reusable Custom Meta Boxes tutorial" from WP Tuts +: http://wp.tutsplus.com/tutorials/reusable-custom-meta-boxes-part-1-intro-and-basic-fields/.

However, I don't know how to add multiple fields within the repeatable fields. This is the idea:

[repeatable]
- Text field: Name of the artist
- Text field: Artist website
- Checkbox: Headliner (Yes or no)
[/repeatable]

This is the code from the tutorial:

function meta_box_callback($fields, $page) { global $post; echo '';

// Begin the field table and loop
echo '<table class="form-table cpt">';
foreach ($fields as $field) {
    // get value of this field if it exists for this post
    if ($field['label'])    $label      = $field['label'];
    if ($field['desc'])     $desc       = '<span class="description">'.$field['desc'].'</span>';
    if ($field['id'])       $id         = $field['id'];
    if ($field['type'])     $type       = $field['type'];
    if ($field['value'])    $value      = $field['value'];
    if ($field['options'])  $options    = $field['options'];

    $meta   = get_post_meta($post->ID, $id, true);
    // begin a table row with
    echo '<tr>
            <th class="cpt_title"><label for="'.$id.'">'.$label.'</label></th>
            <td class="cpt_content">';
            switch($field['type']) {
                case 'repeatable':
                    echo '<a class="repeatable-add button" href="#">+</a>
                            <ul id="'.$field['id'].'-repeatable" class="custom_repeatable">';
                    $i = 0;
                    if ($meta) {
                        foreach($meta as $row) {
                            echo '<li><span class="sort hndle">|||</span>
                                        <input type="text" name="'.$field['id'].'['.$i.']" id="'.$field['id'].'" value="'.$row.'" size="30" />
                                        <a class="repeatable-remove button" href="#">-</a></li>';
                            $i++;
                        }
                    } else {
                        echo '<li><span class="sort hndle">|||</span>
                                    <input type="text" name="'.$field['id'].'['.$i.']" id="'.$field['id'].'" value="" size="30" />
                                    <a class="repeatable-remove button" href="#">-</a></li>';
                    }
                    echo '</ul>
                        <span class="description">'.$field['desc'].'</span>';
                break;
            } // switch
    echo '</td></tr>';
} // foreach
echo '</table>'; // table

} function meta_box_save($post_id, $fields, $page) {

// verify nonce
if (!wp_verify_nonce($_POST[$page.'_meta_box_nonce'], basename(__FILE__))) 
    return $post_id;
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
    return $post_id;
// check permissions
if ($page != $_POST['post_type']) {
    if (!current_user_can('edit_page', $post_id))
        return $post_id;
    } elseif (!current_user_can('edit_post', $post_id)) {
        return $post_id;
}

// loop through fields and save the data
foreach ($fields as $field) {
    if($field['type'] == 'tax_select') {
        // save taxonomies
        $term = $_POST[$field['id']];
        wp_set_object_terms( $post_id, $term, $field['id'] );
    }
    else {
        // save the rest
        $old = get_post_meta($post_id, $field['id'], true);
        $new = $_POST[$field['id']];
        if ($new && $new != $old) {
            update_post_meta($post_id, $field['id'], $new);
        } elseif ('' == $new && $old) {
            delete_post_meta($post_id, $field['id'], $old);
        }
    }
} // foreach

}

Hope you guys can help me out :-).

share|improve this question
1  
try WPALCHEMY! metabox class, easy and flexible. – kamaal Dec 16 '12 at 7:20
well I'm currently working on this issue as well, I already able to generate the group field, it's just still don't know how to save the array for each multifield. If you find any useful resource, please share with me. here is my result pastebin.com/wqfdUMC6 – nackle Jan 4 at 22:11

protected by Community Jan 4 at 20:35

This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.

Browse other questions tagged or ask your own question.