Advertisement
hmbashar

CMB2 repeatable field for option page

Jul 20th, 2017
882
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.18 KB | None | 0 0
  1.  
  2.  
  3. add_action( 'cmb2_admin_init', 'repeatable_news_add_more' );
  4. /**
  5.  * Hook in and register a metabox to handle a theme options page
  6.  */
  7. function repeatable_news_add_more() {
  8.  
  9.     $option_key = 'repeatable-news-options.php';
  10.  
  11.     /**
  12.      * Registers options page menu item and form.
  13.      */
  14.     $morenews = new_cmb2_box( array(
  15.         'id'       => $option_key . 'newsoption',
  16.         'title'    => esc_html__( 'Add News Sections', 'cmb2' ),
  17.         'icon_url' => 'dashicons-palmtree', // Menu icon. Only applicable if 'parent_slug' is left empty.
  18.         'show_on'  => array(
  19.             // Important, don't remove.
  20.             'options-page' => $option_key,
  21.         ),
  22.          'menu_title'      => esc_html__( 'Add Sections', 'cmb2' ), // Falls back to 'title' (above).
  23.          'parent_slug'     => 'cbnews-theme-option.php', // Make options page a submenu item of the themes menu.
  24.          'capability'      => 'manage_options', // Cap required to view options-page.
  25.         // 'position'        => 1, // Menu position. Only applicable if 'parent_slug' is left empty.
  26.         // 'admin_menu_hook' => 'network_admin_menu', // 'network_admin_menu' to add network-level options page.
  27.         // 'display_cb'      => false, // Override the options-page form output (CMB2_Hookup::options_page_output()).
  28.         'save_button'     => esc_html__( 'Save Sections', 'cmb2' ), // The text for the options-page save button. Defaults to 'Save'.
  29.     ) );
  30.  
  31.     // $group_field_id is the field id string, so in this case: $prefix . 'demo'
  32.     $morenews_section = $morenews->add_field( array(
  33.         'id'          => 'news-section',
  34.         'type'        => 'group',
  35.         'description' => esc_html__( 'Generates reusable form entries', 'cmb2' ),
  36.         'options'     => array(
  37.             'group_title'   => esc_html__( 'Entry {#}', 'cmb2' ), // {#} gets replaced by row number
  38.             'add_button'    => esc_html__( 'Add Another Entry', 'cmb2' ),
  39.             'remove_button' => esc_html__( 'Remove Entry', 'cmb2' ),
  40.             'sortable'      => true, // beta
  41.             // 'closed'     => true, // true to have the groups closed by default
  42.         ),
  43.     ) );
  44.  
  45.     /**
  46.      * Group fields works the same, except ids only need
  47.      * to be unique to the group. Prefix is not needed.
  48.      *
  49.      * The parent field's id needs to be passed as the first argument.
  50.      */
  51.     $morenews->add_group_field( $morenews_section, array(
  52.         'name'       => esc_html__( 'Entry Title', 'cmb2' ),
  53.         'id'         => 'title',
  54.         'type'       => 'text',
  55.         // 'repeatable' => true, // Repeatable fields are supported w/in repeatable groups (for most types)
  56.     ) );
  57.  
  58.     $morenews->add_group_field( $morenews_section, array(
  59.         'name'        => esc_html__( 'Description', 'cmb2' ),
  60.         'description' => esc_html__( 'Write a short description for this entry', 'cmb2' ),
  61.         'id'          => 'description',
  62.         'type'        => 'textarea_small',
  63.         //'repeatable' => true,
  64.     ) );
  65.  
  66.     $morenews->add_group_field( $morenews_section, array(
  67.         'name' => esc_html__( 'Entry Image', 'cmb2' ),
  68.         'id'   => 'image',
  69.         'type' => 'file',
  70.     ) );
  71.  
  72.     $morenews->add_group_field( $morenews_section, array(
  73.         'name' => esc_html__( 'Image Caption', 'cmb2' ),
  74.         'id'   => 'image_caption',
  75.         'type' => 'text',
  76.     ) );
  77.     $morenews->add_field(array(
  78.             'name'      => esc_html('Test Text', 'cbnews'),
  79.             'id'        => 'testtext',
  80.             'type'      => 'text'
  81.         ));
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement