yourprefix_register_repeatable_group_field_metabox

Hook in and add a metabox to demonstrate repeatable grouped fields.

Description

yourprefix_register_repeatable_group_field_metabox(); 

Usage

  1. if ( !function_exists( 'yourprefix_register_repeatable_group_field_metabox' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'cmb2/example-functions.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = yourprefix_register_repeatable_group_field_metabox(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/example-functions.php  
  1. function yourprefix_register_repeatable_group_field_metabox() { 
  2. $prefix = 'yourprefix_group_'; 
  3.  
  4. /** 
  5. * Repeatable Field Groups 
  6. */ 
  7. $cmb_group = new_cmb2_box( array( 
  8. 'id' => $prefix . 'metabox',  
  9. 'title' => esc_html__( 'Repeating Field Group', cmb2 ),  
  10. 'object_types' => array( 'page', ),  
  11. ) ); 
  12.  
  13. // $group_field_id is the field id string, so in this case: $prefix . 'demo' 
  14. $group_field_id = $cmb_group->add_field( array( 
  15. 'id' => $prefix . 'demo',  
  16. 'type' => 'group',  
  17. 'description' => esc_html__( 'Generates reusable form entries', cmb2 ),  
  18. 'options' => array( 
  19. 'group_title' => esc_html__( 'Entry {#}', cmb2 ), // {#} gets replaced by row number 
  20. 'add_button' => esc_html__( 'Add Another Entry', cmb2 ),  
  21. 'remove_button' => esc_html__( 'Remove Entry', cmb2 ),  
  22. 'sortable' => true, // beta 
  23. // 'closed' => true, // true to have the groups closed by default 
  24. ),  
  25. ) ); 
  26.  
  27. /** 
  28. * Group fields works the same, except ids only need 
  29. * to be unique to the group. Prefix is not needed. 
  30. * The parent field's id needs to be passed as the first argument. 
  31. */ 
  32. $cmb_group->add_group_field( $group_field_id, array( 
  33. 'name' => esc_html__( 'Entry Title', cmb2 ),  
  34. 'id' => 'title',  
  35. 'type' => 'text',  
  36. // 'repeatable' => true, // Repeatable fields are supported w/in repeatable groups (for most types) 
  37. ) ); 
  38.  
  39. $cmb_group->add_group_field( $group_field_id, array( 
  40. 'name' => esc_html__( 'Description', cmb2 ),  
  41. 'description' => esc_html__( 'Write a short description for this entry', cmb2 ),  
  42. 'id' => 'description',  
  43. 'type' => 'textarea_small',  
  44. ) ); 
  45.  
  46. $cmb_group->add_group_field( $group_field_id, array( 
  47. 'name' => esc_html__( 'Entry Image', cmb2 ),  
  48. 'id' => 'image',  
  49. 'type' => 'file',  
  50. ) ); 
  51.  
  52. $cmb_group->add_group_field( $group_field_id, array( 
  53. 'name' => esc_html__( 'Image Caption', cmb2 ),  
  54. 'id' => 'image_caption',  
  55. 'type' => 'text',  
  56. ) ); 
  57.