Skip to content

Instantly share code, notes, and snippets.

@hirozed
Last active January 17, 2021 05:32
Show Gist options
  • Save hirozed/57f56dc9320481417089deca418cea9d to your computer and use it in GitHub Desktop.
Save hirozed/57f56dc9320481417089deca418cea9d to your computer and use it in GitHub Desktop.
WordPress CMB2 Group
<?php
/**
* Reason for CMB2 box
*/
function create_[cmb2]_group_box() {
// Create the box.
$cmb2 = \new_cmb2_box(
array(
'id' => '[CHANGEME-id]',
'title' => __( '[CHANGEME-title]', '[CHANGEME-localization]' ),
'object_types' => array(
'page',
),
'context' => 'normal',
'priority' => 'default',
'show_names' => true,
'show_on' => array(
'key' => 'page-template',
'value' => array(
'page-templates/[CHANGEME-page-template]',
),
),
)
);
// Add the group field.
$group_field = $cmb2->add_field(
array(
'id' => '_[CHANGEME-id]]',
'type' => 'group',
'description' => __( '', '[CHANGEME-localization]' ),
'repeatable' => true,
'options' => array(
'group_title' => __( 'Item {#}', '[CHANGEME-localization]' ),
'add_button' => __( 'Add Entry', '[CHANGEME-localization]' ),
'remove_button' => __( 'Remove Entry', '[CHANGEME-localization]' ),
'sortable' => true,
'closed' => true,
'remove_confirm' => esc_html__( 'Are you sure you would like to remove this entry?', '[CHANGEME-localization]' ),
),
)
);
// Fields for the box.
$fields = array(
array(
'name' => __( '[CHANGEME-name]', '[CHANGEME-localization]' ),
'id' => '_[CHANGEME-id]',
'type' => '[CHANGEME-type]',
'description' => __( '', '[CHANGEME-localization]' ),
)
);
// Loop through the fields to add to the group.
foreach ( $fields as $field ) {
$cmb2->add_group_field( $group_field, $field );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment