Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action( 'cmb2_admin_init', 'repeatable_news_add_more' );
- /**
- * Hook in and register a metabox to handle a theme options page
- */
- function repeatable_news_add_more() {
- $option_key = 'repeatable-news-options.php';
- /**
- * Registers options page menu item and form.
- */
- $morenews = new_cmb2_box( array(
- 'id' => $option_key . 'newsoption',
- 'title' => esc_html__( 'Add News Sections', 'cmb2' ),
- 'icon_url' => 'dashicons-palmtree', // Menu icon. Only applicable if 'parent_slug' is left empty.
- 'show_on' => array(
- // Important, don't remove.
- 'options-page' => $option_key,
- ),
- 'menu_title' => esc_html__( 'Add Sections', 'cmb2' ), // Falls back to 'title' (above).
- 'parent_slug' => 'cbnews-theme-option.php', // Make options page a submenu item of the themes menu.
- 'capability' => 'manage_options', // Cap required to view options-page.
- // 'position' => 1, // Menu position. Only applicable if 'parent_slug' is left empty.
- // 'admin_menu_hook' => 'network_admin_menu', // 'network_admin_menu' to add network-level options page.
- // 'display_cb' => false, // Override the options-page form output (CMB2_Hookup::options_page_output()).
- 'save_button' => esc_html__( 'Save Sections', 'cmb2' ), // The text for the options-page save button. Defaults to 'Save'.
- ) );
- // $group_field_id is the field id string, so in this case: $prefix . 'demo'
- $morenews_section = $morenews->add_field( array(
- 'id' => 'news-section',
- 'type' => 'group',
- 'description' => esc_html__( 'Generates reusable form entries', 'cmb2' ),
- 'options' => array(
- 'group_title' => esc_html__( 'Entry {#}', 'cmb2' ), // {#} gets replaced by row number
- 'add_button' => esc_html__( 'Add Another Entry', 'cmb2' ),
- 'remove_button' => esc_html__( 'Remove Entry', 'cmb2' ),
- 'sortable' => true, // beta
- // 'closed' => true, // true to have the groups closed by default
- ),
- ) );
- /**
- * Group fields works the same, except ids only need
- * to be unique to the group. Prefix is not needed.
- *
- * The parent field's id needs to be passed as the first argument.
- */
- $morenews->add_group_field( $morenews_section, array(
- 'name' => esc_html__( 'Entry Title', 'cmb2' ),
- 'id' => 'title',
- 'type' => 'text',
- // 'repeatable' => true, // Repeatable fields are supported w/in repeatable groups (for most types)
- ) );
- $morenews->add_group_field( $morenews_section, array(
- 'name' => esc_html__( 'Description', 'cmb2' ),
- 'description' => esc_html__( 'Write a short description for this entry', 'cmb2' ),
- 'id' => 'description',
- 'type' => 'textarea_small',
- //'repeatable' => true,
- ) );
- $morenews->add_group_field( $morenews_section, array(
- 'name' => esc_html__( 'Entry Image', 'cmb2' ),
- 'id' => 'image',
- 'type' => 'file',
- ) );
- $morenews->add_group_field( $morenews_section, array(
- 'name' => esc_html__( 'Image Caption', 'cmb2' ),
- 'id' => 'image_caption',
- 'type' => 'text',
- ) );
- $morenews->add_field(array(
- 'name' => esc_html('Test Text', 'cbnews'),
- 'id' => 'testtext',
- 'type' => 'text'
- ));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement