• Resolved ogdsgn

    (@ogdsgn)


    Wondering how I would approach creating some default entries within the repeatable group type?

    ie. I have a Meta Box “Meta Group” with a repeatable group of a text input & text area, and I would like to be able to have a few defaults populated, with the option to create more.

    Here’s my code.

    $meta_boxes['meta_group'] = array(
        'id'           => 'meta_group',
        'title'        => __( 'Meta Group', 'cmb2' ),
        'object_types' => array( 'post', ),
        'closed'       => true,
        'fields'       => array(
            array(
                'id'          => $prefix . 'meta_group',
                'type'        => 'group',
                'options'     => array(
                    'group_title'   => __( 'Meta Group {#}', 'cmb2' ),
                    'add_button'    => __( 'Add Paragraph', 'cmb2' ),
                    'remove_button' => __( 'Remove Paragraph', 'cmb2' ),
                    'sortable'      => false, // beta
                ),
                'fields'      => array(
                    array(
                        'name' => 'Title',
                        'default' => 'Title ',
                        'id'   => 'title',
                        'type' => 'text',
                    ),
                    array(
                        'name' => 'Description',
                        'id'   => 'description',
                        'type' => 'textarea',
                    ),
                ),
            ),
        ),
    );

    https://ww.wp.xz.cn/plugins/cmb2/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Well, you already have a default provided for the title field, the same method can be used to add a default to the textarea.

    Once you click “Add Paragraph” it repeats all the same fields with the same defaults as well.

    Try it out.

    Thread Starter ogdsgn

    (@ogdsgn)

    Thanks Michael,

    I was able to get the default inputs for both Title & description, however, I was wondering if it’s possible to create multiple defaults of the Meta_Group Paragraph (the one which is repeatable and contains the Title&description.

    So the result would look something like this.

    Meta_Group Box
    1stParagraph
    2ndParagraph
    3rdParagraph
    4thParagraph
    Add New Paragraph

    Does that make sense?

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    I believe just adding the following to each field would work:

    'default' => 'default value'

    Not sure about how to best handle any numeral increments to the text offhand, but if that’s not a necessary part, then what’s above should work fine.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Creating Default Entries within a Repeatable Group’ is closed to new replies.