项目作者: DanielDrabik

项目描述 :
Easier, quicker and cleaner way to create CMB2 Tabs and Fields.
高级语言: PHP
项目地址: git://github.com/DanielDrabik/CMB2-Helper.git
创建时间: 2017-11-16T11:34:13Z
项目社区:https://github.com/DanielDrabik/CMB2-Helper

开源协议:

下载


CMB2-Helper

Easier, quicker and cleaner way to create CMB2 Tabs and Fields.

Index

What is this for?

Creating custom fields with CMB2 (while using tabs*) can be painful. Code gets bigger and dirtier with each field and tab added.

CMB2-Helper is a simple sollution for that problem. It creates arrays of tabs and fields in background, while you use just few methods.

Example:

Let’s say you want to add to your front-page editor a metabox with 3 tabs and few fields for each tab.
You will probably end with code looking like this:

  1. <?php
  2. add_action('cmb2_admin_init', 'custom_prefix_page');
  3. function custom_prefix_page() {
  4. page_();
  5. }
  6. function front_page_() {
  7. $prefix = __FUNCTION__;
  8. $box_options = array(
  9. 'id' => $prefix . 'metabox',
  10. 'title' => esc_html__('Front Page Settings', 'cmb2'),
  11. 'object_types' => array('page',),
  12. 'show_on' => array('id' => array(get_option('page_on_front'),)),
  13. 'closed' => false,
  14. );
  15. $cmb = new_cmb2_box( $box_options );
  16. $tabs_setting = array(
  17. 'config' => $box_options,
  18. 'layout' => 'vertical', // Default : horizontal
  19. 'tabs' => array()
  20. );
  21. $id = "1_";
  22. $tabs_setting['tabs'][] = array(
  23. 'id' => $id,
  24. 'title' => __( 'Section 1', 'cmb2' ),
  25. 'fields' => array(
  26. array(
  27. 'name' => __( 'Image backgorund in header', 'cmb2' ),
  28. 'id' => $id . 'main_image',
  29. 'type' => 'file'
  30. ),
  31. array(
  32. 'name' => __( 'Text on header', 'cmb2' ),
  33. 'id' => $id . 'main_text',
  34. 'type' => 'wysiwyg'
  35. ),
  36. array(
  37. 'name' => __( 'Contributors', 'cmb2' ),
  38. 'id' => $id . 'contributors',
  39. 'type' => 'group',
  40. 'fields' => array(
  41. array(
  42. 'name' => __( 'Photo', 'cmb2' ),
  43. 'id' => 'photo',
  44. 'type' => 'file'
  45. ),
  46. array(
  47. 'name' => __( 'Name', 'cmb2' ),
  48. 'id' => 'name',
  49. 'type' => 'text'
  50. ),
  51. array(
  52. 'name' => __( 'Biography', 'cmb2' ),
  53. 'id' => 'biography',
  54. 'type' => 'wysiwyg'
  55. ),
  56. ),
  57. ),
  58. array(
  59. 'name' => __( 'Email Address', 'cmb2' ),
  60. 'id' => $id . 'email',
  61. 'type' => 'text'
  62. ),
  63. array(
  64. 'name' => __( 'Background Color of section', 'cmb2' ),
  65. 'id' => $id . 'background',
  66. 'type' => 'select',
  67. 'options' => array(
  68. '#fff' => __( 'White', 'cmb2' ),
  69. '#000' => __( 'Black', 'cmb2' ),
  70. ),
  71. ),
  72. array(
  73. 'name' => __( 'Greeting message', 'cmb2' ),
  74. 'id' => $id . 'message',
  75. 'type' => 'text',
  76. 'default' => 'Hey my friend! :)'
  77. ),
  78. )
  79. );
  80. $id = "2_";
  81. $tabs_setting['tabs'][] = array(
  82. 'id' => $id,
  83. 'title' => __( 'Section 2', 'cmb2' ),
  84. 'fields' => array(
  85. array(
  86. 'name' => __( 'Title on black area', 'cmb2' ),
  87. 'id' => $id . 'title',
  88. 'type' => 'text',
  89. ),
  90. array(
  91. 'name' => __( 'Text on black area', 'cmb2' ),
  92. 'id' => $id . 'text',
  93. 'type' => 'wysiwyg',
  94. ),
  95. )
  96. );
  97. $id = "3_";
  98. $tabs_setting['tabs'][] = array(
  99. 'id' => $id,
  100. 'title' => __( 'Section 3', 'cmb2' ),
  101. 'fields' => array(
  102. array(
  103. 'name' => __( 'Text on the left', 'cmb2' ),
  104. 'id' => $id . 'text_left',
  105. 'type' => 'wysiwyg',
  106. ),
  107. array(
  108. 'name' => __( 'Text on the right', 'cmb2' ),
  109. 'id' => $id . 'text_right',
  110. 'type' => 'wysiwyg',
  111. ),
  112. )
  113. );
  114. $cmb->add_field( array(
  115. 'id' => '__tabs',
  116. 'type' => 'tabs',
  117. 'tabs' => $tabs_setting
  118. ) );
  119. }

That’s freaking 130 lines of code for 13 fields and 3 tabs. In my opinion it’s way too much.
Here is the code made with CMB2-Helper:

  1. <?php
  2. add_action('cmb2_admin_init', 'custom_prefix_page');
  3. function custom_prefix_page() {
  4. page_();
  5. }
  6. function page_() {
  7. $prefix = __FUNCTION__;
  8. $type = array('page');
  9. $show_on = array('id' => array(get_option('page_on_front'),));
  10. $cmb_helper = new cmbField($prefix, 'Front Page Settings', $type, $show_on);
  11. $cmb_helper->addTab('Section 1');
  12. $cmb_helper->addField('main_image', 'Image backgorund in header', 'file');
  13. $cmb_helper->addField('main_text', 'Text on header', 'wysiwyg');
  14. $cmb_helper->addField('contributors', 'Contributors', 'group');
  15. $cmb_helper->addField('photo', 'Photo', 'file', 'contributors');
  16. $cmb_helper->addField('name', 'Name', 'text', 'contributors');
  17. $cmb_helper->addField('biography', 'Biography', 'wysiwyg', 'contributors');
  18. $cmb_helper->addField('email', 'Email Address', 'text');
  19. $options = array(
  20. '#fff' => __( 'White', 'cmb2' ),
  21. '#000' => __( 'Black', 'cmb2' ),
  22. );
  23. $cmb_helper->addField('background', 'Background Color of section', 'select', false, $options);
  24. $cmb_helper->addField('message', 'Greetings message', 'text', false, false, 'Hey my friend! :)');
  25. $cmb_helper->addTab('Section 2');
  26. $cmb_helper->addField('title', 'Title on black area', 'text');
  27. $cmb_helper->addField('text', 'Text on black area', 'wysiwyg');
  28. $cmb_helper->addTab('Section 3');
  29. $cmb_helper->addField('text_left', 'Text on the left', 'wysiwyg');
  30. $cmb_helper->addField('text_right', 'Text on the right', 'wysiwyg');
  31. $cmb = new_cmb2_box($cmb_helper->generateCMB());
  32. $cmb->add_field($cmb_helper->generateTabs());
  33. }

We end up with only 43 lines. Much better, isn’t it?

Requirements:

Installation:

  1. Add files from the repository to your theme’s directory
  2. Add:
    1. include 'cmb-helper.php';
    To your functions.php
  3. Now you can start creating custom fields much quicker and simpler