yourprefix_register_demo_metabox

Hook in and add a demo metabox.

Description

yourprefix_register_demo_metabox(); 

Can only happen on the cmb2_admin_init or cmb2_init hook.


Usage

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

Defined (1)

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

/example-functions.php  
  1. function yourprefix_register_demo_metabox() { 
  2. $prefix = 'yourprefix_demo_'; 
  3.  
  4. /** 
  5. * Sample metabox to demonstrate each field type included 
  6. */ 
  7. $cmb_demo = new_cmb2_box( array( 
  8. 'id' => $prefix . 'metabox',  
  9. 'title' => esc_html__( 'Test Metabox', cmb2 ),  
  10. 'object_types' => array( 'page', ), // Post type 
  11. // 'show_on_cb' => 'yourprefix_show_if_front_page', // function should return a bool value 
  12. // 'context' => 'normal',  
  13. // 'priority' => 'high',  
  14. // 'show_names' => true, // Show field names on the left 
  15. // 'cmb_styles' => false, // false to disable the CMB stylesheet 
  16. // 'closed' => true, // true to keep the metabox closed by default 
  17. // 'classes' => 'extra-class', // Extra cmb2-wrap classes 
  18. // 'classes_cb' => 'yourprefix_add_some_classes', // Add classes through a callback. 
  19. ) ); 
  20.  
  21. $cmb_demo->add_field( array( 
  22. 'name' => esc_html__( 'Test Text', cmb2 ),  
  23. 'desc' => esc_html__( 'field description (optional)', cmb2 ),  
  24. 'id' => $prefix . 'text',  
  25. 'type' => 'text',  
  26. 'show_on_cb' => 'yourprefix_hide_if_no_cats', // function should return a bool value 
  27. // 'sanitization_cb' => 'my_custom_sanitization', // custom sanitization callback parameter 
  28. // 'escape_cb' => 'my_custom_escaping', // custom escaping callback parameter 
  29. // 'on_front' => false, // Optionally designate a field to wp-admin only 
  30. // 'repeatable' => true,  
  31. // 'column' => true, // Display field value in the admin post-listing columns 
  32. ) ); 
  33.  
  34. $cmb_demo->add_field( array( 
  35. 'name' => esc_html__( 'Test Text Small', cmb2 ),  
  36. 'desc' => esc_html__( 'field description (optional)', cmb2 ),  
  37. 'id' => $prefix . 'textsmall',  
  38. 'type' => 'text_small',  
  39. // 'repeatable' => true,  
  40. // 'column' => array( 
  41. // 'name' => esc_html__( 'Column Title', cmb2 ), // Set the admin column title 
  42. // 'position' => 2, // Set as the second column. 
  43. // ); 
  44. // 'display_cb' => 'yourprefix_display_text_small_column', // Output the display of the column values through a callback. 
  45. ) ); 
  46.  
  47. $cmb_demo->add_field( array( 
  48. 'name' => esc_html__( 'Test Text Medium', cmb2 ),  
  49. 'desc' => esc_html__( 'field description (optional)', cmb2 ),  
  50. 'id' => $prefix . 'textmedium',  
  51. 'type' => 'text_medium',  
  52. ) ); 
  53.  
  54. $cmb_demo->add_field( array( 
  55. 'name' => esc_html__( 'Read-only Disabled Field', cmb2 ),  
  56. 'desc' => esc_html__( 'field description (optional)', cmb2 ),  
  57. 'id' => $prefix . 'readonly',  
  58. 'type' => 'text_medium',  
  59. 'default' => esc_attr__( 'Hey there, I\'m a read-only field', cmb2 ),  
  60. 'save_field' => false, // Disables the saving of this field. 
  61. 'attributes' => array( 
  62. 'disabled' => 'disabled',  
  63. 'readonly' => 'readonly',  
  64. ),  
  65. ) ); 
  66.  
  67. $cmb_demo->add_field( array( 
  68. 'name' => esc_html__( 'Custom Rendered Field', cmb2 ),  
  69. 'desc' => esc_html__( 'field description (optional)', cmb2 ),  
  70. 'id' => $prefix . 'render_row_cb',  
  71. 'type' => 'text',  
  72. 'render_row_cb' => 'yourprefix_render_row_cb',  
  73. ) ); 
  74.  
  75. $cmb_demo->add_field( array( 
  76. 'name' => esc_html__( 'Website URL', cmb2 ),  
  77. 'desc' => esc_html__( 'field description (optional)', cmb2 ),  
  78. 'id' => $prefix . 'url',  
  79. 'type' => 'text_url',  
  80. // 'protocols' => array('http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet'), // Array of allowed protocols 
  81. // 'repeatable' => true,  
  82. ) ); 
  83.  
  84. $cmb_demo->add_field( array( 
  85. 'name' => esc_html__( 'Test Text Email', cmb2 ),  
  86. 'desc' => esc_html__( 'field description (optional)', cmb2 ),  
  87. 'id' => $prefix . 'email',  
  88. 'type' => 'text_email',  
  89. // 'repeatable' => true,  
  90. ) ); 
  91.  
  92. $cmb_demo->add_field( array( 
  93. 'name' => esc_html__( 'Test Time', cmb2 ),  
  94. 'desc' => esc_html__( 'field description (optional)', cmb2 ),  
  95. 'id' => $prefix . 'time',  
  96. 'type' => 'text_time',  
  97. // 'time_format' => 'H:i', // Set to 24hr format 
  98. ) ); 
  99.  
  100. $cmb_demo->add_field( array( 
  101. 'name' => esc_html__( 'Time zone', cmb2 ),  
  102. 'desc' => esc_html__( 'Time zone', cmb2 ),  
  103. 'id' => $prefix . 'timezone',  
  104. 'type' => 'select_timezone',  
  105. ) ); 
  106.  
  107. $cmb_demo->add_field( array( 
  108. 'name' => esc_html__( 'Test Date Picker', cmb2 ),  
  109. 'desc' => esc_html__( 'field description (optional)', cmb2 ),  
  110. 'id' => $prefix . 'textdate',  
  111. 'type' => 'text_date',  
  112. // 'date_format' => 'Y-m-d',  
  113. ) ); 
  114.  
  115. $cmb_demo->add_field( array( 
  116. 'name' => esc_html__( 'Test Date Picker (UNIX timestamp)', cmb2 ),  
  117. 'desc' => esc_html__( 'field description (optional)', cmb2 ),  
  118. 'id' => $prefix . 'textdate_timestamp',  
  119. 'type' => 'text_date_timestamp',  
  120. // 'timezone_meta_key' => $prefix . 'timezone', // Optionally make this field honor the timezone selected in the select_timezone specified above 
  121. ) ); 
  122.  
  123. $cmb_demo->add_field( array( 
  124. 'name' => esc_html__( 'Test Date/Time Picker Combo (UNIX timestamp)', cmb2 ),  
  125. 'desc' => esc_html__( 'field description (optional)', cmb2 ),  
  126. 'id' => $prefix . 'datetime_timestamp',  
  127. 'type' => 'text_datetime_timestamp',  
  128. ) ); 
  129.  
  130. // This text_datetime_timestamp_timezone field type 
  131. // is only compatible with PHP versions 5.3 or above. 
  132. // Feel free to uncomment and use if your server meets the requirement 
  133. // $cmb_demo->add_field( array( 
  134. // 'name' => esc_html__( 'Test Date/Time Picker/Time zone Combo (serialized DateTime object)', cmb2 ),  
  135. // 'desc' => esc_html__( 'field description (optional)', cmb2 ),  
  136. // 'id' => $prefix . 'datetime_timestamp_timezone',  
  137. // 'type' => 'text_datetime_timestamp_timezone',  
  138. // ) ); 
  139.  
  140. $cmb_demo->add_field( array( 
  141. 'name' => esc_html__( 'Test Money', cmb2 ),  
  142. 'desc' => esc_html__( 'field description (optional)', cmb2 ),  
  143. 'id' => $prefix . 'textmoney',  
  144. 'type' => 'text_money',  
  145. // 'before_field' => '', // override '$' symbol if needed 
  146. // 'repeatable' => true,  
  147. ) ); 
  148.  
  149. $cmb_demo->add_field( array( 
  150. 'name' => esc_html__( 'Test Color Picker', cmb2 ),  
  151. 'desc' => esc_html__( 'field description (optional)', cmb2 ),  
  152. 'id' => $prefix . 'colorpicker',  
  153. 'type' => 'colorpicker',  
  154. 'default' => '#ffffff',  
  155. // 'attributes' => array( 
  156. // 'data-colorpicker' => json_encode( array( 
  157. // 'palettes' => array( '#3dd0cc', '#ff834c', '#4fa2c0', '#0bc991', ),  
  158. // ) ),  
  159. // ),  
  160. ) ); 
  161.  
  162. $cmb_demo->add_field( array( 
  163. 'name' => esc_html__( 'Test Text Area', cmb2 ),  
  164. 'desc' => esc_html__( 'field description (optional)', cmb2 ),  
  165. 'id' => $prefix . 'textarea',  
  166. 'type' => 'textarea',  
  167. ) ); 
  168.  
  169. $cmb_demo->add_field( array( 
  170. 'name' => esc_html__( 'Test Text Area Small', cmb2 ),  
  171. 'desc' => esc_html__( 'field description (optional)', cmb2 ),  
  172. 'id' => $prefix . 'textareasmall',  
  173. 'type' => 'textarea_small',  
  174. ) ); 
  175.  
  176. $cmb_demo->add_field( array( 
  177. 'name' => esc_html__( 'Test Text Area for Code', cmb2 ),  
  178. 'desc' => esc_html__( 'field description (optional)', cmb2 ),  
  179. 'id' => $prefix . 'textarea_code',  
  180. 'type' => 'textarea_code',  
  181. ) ); 
  182.  
  183. $cmb_demo->add_field( array( 
  184. 'name' => esc_html__( 'Test Title Weeeee', cmb2 ),  
  185. 'desc' => esc_html__( 'This is a title description', cmb2 ),  
  186. 'id' => $prefix . 'title',  
  187. 'type' => 'title',  
  188. ) ); 
  189.  
  190. $cmb_demo->add_field( array( 
  191. 'name' => esc_html__( 'Test Select', cmb2 ),  
  192. 'desc' => esc_html__( 'field description (optional)', cmb2 ),  
  193. 'id' => $prefix . 'select',  
  194. 'type' => 'select',  
  195. 'show_option_none' => true,  
  196. 'options' => array( 
  197. 'standard' => esc_html__( 'Option One', cmb2 ),  
  198. 'custom' => esc_html__( 'Option Two', cmb2 ),  
  199. 'none' => esc_html__( 'Option Three', cmb2 ),  
  200. ),  
  201. ) ); 
  202.  
  203. $cmb_demo->add_field( array( 
  204. 'name' => esc_html__( 'Test Radio inline', cmb2 ),  
  205. 'desc' => esc_html__( 'field description (optional)', cmb2 ),  
  206. 'id' => $prefix . 'radio_inline',  
  207. 'type' => 'radio_inline',  
  208. 'show_option_none' => 'No Selection',  
  209. 'options' => array( 
  210. 'standard' => esc_html__( 'Option One', cmb2 ),  
  211. 'custom' => esc_html__( 'Option Two', cmb2 ),  
  212. 'none' => esc_html__( 'Option Three', cmb2 ),  
  213. ),  
  214. ) ); 
  215.  
  216. $cmb_demo->add_field( array( 
  217. 'name' => esc_html__( 'Test Radio', cmb2 ),  
  218. 'desc' => esc_html__( 'field description (optional)', cmb2 ),  
  219. 'id' => $prefix . 'radio',  
  220. 'type' => 'radio',  
  221. 'options' => array( 
  222. 'option1' => esc_html__( 'Option One', cmb2 ),  
  223. 'option2' => esc_html__( 'Option Two', cmb2 ),  
  224. 'option3' => esc_html__( 'Option Three', cmb2 ),  
  225. ),  
  226. ) ); 
  227.  
  228. $cmb_demo->add_field( array( 
  229. 'name' => esc_html__( 'Test Taxonomy Radio', cmb2 ),  
  230. 'desc' => esc_html__( 'field description (optional)', cmb2 ),  
  231. 'id' => $prefix . 'text_taxonomy_radio',  
  232. 'type' => 'taxonomy_radio',  
  233. 'taxonomy' => 'category', // Taxonomy Slug 
  234. // 'inline' => true, // Toggles display to inline 
  235. ) ); 
  236.  
  237. $cmb_demo->add_field( array( 
  238. 'name' => esc_html__( 'Test Taxonomy Select', cmb2 ),  
  239. 'desc' => esc_html__( 'field description (optional)', cmb2 ),  
  240. 'id' => $prefix . 'taxonomy_select',  
  241. 'type' => 'taxonomy_select',  
  242. 'taxonomy' => 'category', // Taxonomy Slug 
  243. ) ); 
  244.  
  245. $cmb_demo->add_field( array( 
  246. 'name' => esc_html__( 'Test Taxonomy Multi Checkbox', cmb2 ),  
  247. 'desc' => esc_html__( 'field description (optional)', cmb2 ),  
  248. 'id' => $prefix . 'multitaxonomy',  
  249. 'type' => 'taxonomy_multicheck',  
  250. 'taxonomy' => 'post_tag', // Taxonomy Slug 
  251. // 'inline' => true, // Toggles display to inline 
  252. ) ); 
  253.  
  254. $cmb_demo->add_field( array( 
  255. 'name' => esc_html__( 'Test Checkbox', cmb2 ),  
  256. 'desc' => esc_html__( 'field description (optional)', cmb2 ),  
  257. 'id' => $prefix . 'checkbox',  
  258. 'type' => 'checkbox',  
  259. ) ); 
  260.  
  261. $cmb_demo->add_field( array( 
  262. 'name' => esc_html__( 'Test Multi Checkbox', cmb2 ),  
  263. 'desc' => esc_html__( 'field description (optional)', cmb2 ),  
  264. 'id' => $prefix . 'multicheckbox',  
  265. 'type' => 'multicheck',  
  266. // 'multiple' => true, // Store values in individual rows 
  267. 'options' => array( 
  268. 'check1' => esc_html__( 'Check One', cmb2 ),  
  269. 'check2' => esc_html__( 'Check Two', cmb2 ),  
  270. 'check3' => esc_html__( 'Check Three', cmb2 ),  
  271. ),  
  272. // 'inline' => true, // Toggles display to inline 
  273. ) ); 
  274.  
  275. $cmb_demo->add_field( array( 
  276. 'name' => esc_html__( 'Test wysiwyg', cmb2 ),  
  277. 'desc' => esc_html__( 'field description (optional)', cmb2 ),  
  278. 'id' => $prefix . 'wysiwyg',  
  279. 'type' => 'wysiwyg',  
  280. 'options' => array( 'textarea_rows' => 5, ),  
  281. ) ); 
  282.  
  283. $cmb_demo->add_field( array( 
  284. 'name' => esc_html__( 'Test Image', cmb2 ),  
  285. 'desc' => esc_html__( 'Upload an image or enter a URL.', cmb2 ),  
  286. 'id' => $prefix . 'image',  
  287. 'type' => 'file',  
  288. ) ); 
  289.  
  290. $cmb_demo->add_field( array( 
  291. 'name' => esc_html__( 'Multiple Files', cmb2 ),  
  292. 'desc' => esc_html__( 'Upload or add multiple images/attachments.', cmb2 ),  
  293. 'id' => $prefix . 'file_list',  
  294. 'type' => 'file_list',  
  295. 'preview_size' => array( 100, 100 ), // Default: array( 50, 50 ) 
  296. ) ); 
  297.  
  298. $cmb_demo->add_field( array( 
  299. 'name' => esc_html__( 'oEmbed', cmb2 ),  
  300. 'desc' => sprintf( 
  301. /** translators: %s: link to codex.wordpress.org/Embeds */ 
  302. esc_html__( 'Enter a youtube, twitter, or instagram URL. Supports services listed at %s.', cmb2 ),  
  303. '<a href="https://codex.wordpress.org/Embeds">codex.wordpress.org/Embeds</a>' 
  304. ),  
  305. 'id' => $prefix . 'embed',  
  306. 'type' => 'oembed',  
  307. ) ); 
  308.  
  309. $cmb_demo->add_field( array( 
  310. 'name' => 'Testing Field Parameters',  
  311. 'id' => $prefix . 'parameters',  
  312. 'type' => 'text',  
  313. 'before_row' => 'yourprefix_before_row_if_2', // callback 
  314. 'before' => '<p>Testing <b>"before"</b> parameter</p>',  
  315. 'before_field' => '<p>Testing <b>"before_field"</b> parameter</p>',  
  316. 'after_field' => '<p>Testing <b>"after_field"</b> parameter</p>',  
  317. 'after' => '<p>Testing <b>"after"</b> parameter</p>',  
  318. 'after_row' => '<p>Testing <b>"after_row"</b> parameter</p>',  
  319. ) ); 
  320.