PluginProbe
Bookit — Booking & Appointment Calendar / 1.2
Bookit — Booking & Appointment Calendar v1.2
2.6.0.5 2.6.0.4 2.6.0.3 2.6.0.2 2.6.0.1 2.6.0 trunk 1.2 1.2.2 1.2.3 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 All 62 releases
bookit / cmb2 / example-functions.php

example-functions.php in Bookit — Booking & Appointment Calendar 1.2, at cmb2/example-functions.php

775 lines 27.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Include and setup custom metaboxes and fields. (make sure you copy this file to outside the CMB2 directory)
4 *
5 * Be sure to replace all instances of 'yourprefix_' with your project's prefix.
6 * http://nacin.com/2010/05/11/in-wordpress-prefix-everything/
7 *
8 * @category YourThemeOrPlugin
9 * @package Demo_CMB2
10 * @license http://www.opensource.org/licenses/gpl-license.php GPL v2.0 (or later)
11 * @link https://github.com/CMB2/CMB2
12 */
13
14 /**
15 * Get the bootstrap! If using the plugin from wordpress.org, REMOVE THIS!
16 */
17
18 if ( file_exists( dirname( __FILE__ ) . '/cmb2/init.php' ) ) {
19 require_once dirname( __FILE__ ) . '/cmb2/init.php';
20 } elseif ( file_exists( dirname( __FILE__ ) . '/CMB2/init.php' ) ) {
21 require_once dirname( __FILE__ ) . '/CMB2/init.php';
22 }
23
24 /**
25 * Conditionally displays a metabox when used as a callback in the 'show_on_cb' cmb2_box parameter
26 *
27 * @param CMB2 $cmb CMB2 object.
28 *
29 * @return bool True if metabox should show
30 */
31 function yourprefix_show_if_front_page( $cmb ) {
32 // Don't show this metabox if it's not the front page template.
33 if ( get_option( 'page_on_front' ) !== $cmb->object_id ) {
34 return false;
35 }
36 return true;
37 }
38
39 /**
40 * Conditionally displays a field when used as a callback in the 'show_on_cb' field parameter
41 *
42 * @param CMB2_Field $field Field object.
43 *
44 * @return bool True if metabox should show
45 */
46 function yourprefix_hide_if_no_cats( $field ) {
47 // Don't show this field if not in the cats category.
48 if ( ! has_tag( 'cats', $field->object_id ) ) {
49 return false;
50 }
51 return true;
52 }
53
54 /**
55 * Manually render a field.
56 *
57 * @param array $field_args Array of field arguments.
58 * @param CMB2_Field $field The field object.
59 */
60 function yourprefix_render_row_cb( $field_args, $field ) {
61 $classes = $field->row_classes();
62 $id = $field->args( 'id' );
63 $label = $field->args( 'name' );
64 $name = $field->args( '_name' );
65 $value = $field->escaped_value();
66 $description = $field->args( 'description' );
67 ?>
68 <div class="custom-field-row <?php echo esc_attr( $classes ); ?>">
69 <p><label for="<?php echo esc_attr( $id ); ?>"><?php echo esc_html( $label ); ?></label></p>
70 <p><input id="<?php echo esc_attr( $id ); ?>" type="text" name="<?php echo esc_attr( $name ); ?>" value="<?php echo $value; ?>"/></p>
71 <p class="description"><?php echo esc_html( $description ); ?></p>
72 </div>
73 <?php
74 }
75
76 /**
77 * Manually render a field column display.
78 *
79 * @param array $field_args Array of field arguments.
80 * @param CMB2_Field $field The field object.
81 */
82 function yourprefix_display_text_small_column( $field_args, $field ) {
83 ?>
84 <div class="custom-column-display <?php echo esc_attr( $field->row_classes() ); ?>">
85 <p><?php echo $field->escaped_value(); ?></p>
86 <p class="description"><?php echo esc_html( $field->args( 'description' ) ); ?></p>
87 </div>
88 <?php
89 }
90
91 /**
92 * Conditionally displays a message if the $post_id is 2
93 *
94 * @param array $field_args Array of field parameters.
95 * @param CMB2_Field $field Field object.
96 */
97 function yourprefix_before_row_if_2( $field_args, $field ) {
98 if ( 2 == $field->object_id ) {
99 echo '<p>Testing <b>"before_row"</b> parameter (on $post_id 2)</p>';
100 } else {
101 echo '<p>Testing <b>"before_row"</b> parameter (<b>NOT</b> on $post_id 2)</p>';
102 }
103 }
104
105 add_action( 'cmb2_admin_init', 'yourprefix_register_demo_metabox' );
106 /**
107 * Hook in and add a demo metabox. Can only happen on the 'cmb2_admin_init' or 'cmb2_init' hook.
108 */
109 function yourprefix_register_demo_metabox() {
110 $prefix = 'yourprefix_demo_';
111
112 /**
113 * Sample metabox to demonstrate each field type included
114 */
115 $cmb_demo = new_cmb2_box( array(
116 'id' => $prefix . 'metabox',
117 'title' => esc_html__( 'Test Metabox', 'cmb2' ),
118 'object_types' => array( 'page' ), // Post type
119 // 'show_on_cb' => 'yourprefix_show_if_front_page', // function should return a bool value
120 // 'context' => 'normal',
121 // 'priority' => 'high',
122 // 'show_names' => true, // Show field names on the left
123 // 'cmb_styles' => false, // false to disable the CMB stylesheet
124 // 'closed' => true, // true to keep the metabox closed by default
125 // 'classes' => 'extra-class', // Extra cmb2-wrap classes
126 // 'classes_cb' => 'yourprefix_add_some_classes', // Add classes through a callback.
127 ) );
128
129 $cmb_demo->add_field( array(
130 'name' => esc_html__( 'Test Text', 'cmb2' ),
131 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
132 'id' => $prefix . 'text',
133 'type' => 'text',
134 'show_on_cb' => 'yourprefix_hide_if_no_cats', // function should return a bool value
135 // 'sanitization_cb' => 'my_custom_sanitization', // custom sanitization callback parameter
136 // 'escape_cb' => 'my_custom_escaping', // custom escaping callback parameter
137 // 'on_front' => false, // Optionally designate a field to wp-admin only
138 // 'repeatable' => true,
139 // 'column' => true, // Display field value in the admin post-listing columns
140 ) );
141
142 $cmb_demo->add_field( array(
143 'name' => esc_html__( 'Test Text Small', 'cmb2' ),
144 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
145 'id' => $prefix . 'textsmall',
146 'type' => 'text_small',
147 // 'repeatable' => true,
148 // 'column' => array(
149 // 'name' => esc_html__( 'Column Title', 'cmb2' ), // Set the admin column title
150 // 'position' => 2, // Set as the second column.
151 // );
152 // 'display_cb' => 'yourprefix_display_text_small_column', // Output the display of the column values through a callback.
153 ) );
154
155 $cmb_demo->add_field( array(
156 'name' => esc_html__( 'Test Text Medium', 'cmb2' ),
157 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
158 'id' => $prefix . 'textmedium',
159 'type' => 'text_medium',
160 ) );
161
162 $cmb_demo->add_field( array(
163 'name' => esc_html__( 'Read-only Disabled Field', 'cmb2' ),
164 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
165 'id' => $prefix . 'readonly',
166 'type' => 'text_medium',
167 'default' => esc_attr__( 'Hey there, I\'m a read-only field', 'cmb2' ),
168 'save_field' => false, // Disables the saving of this field.
169 'attributes' => array(
170 'disabled' => 'disabled',
171 'readonly' => 'readonly',
172 ),
173 ) );
174
175 $cmb_demo->add_field( array(
176 'name' => esc_html__( 'Custom Rendered Field', 'cmb2' ),
177 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
178 'id' => $prefix . 'render_row_cb',
179 'type' => 'text',
180 'render_row_cb' => 'yourprefix_render_row_cb',
181 ) );
182
183 $cmb_demo->add_field( array(
184 'name' => esc_html__( 'Website URL', 'cmb2' ),
185 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
186 'id' => $prefix . 'url',
187 'type' => 'text_url',
188 // 'protocols' => array('http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet'), // Array of allowed protocols
189 // 'repeatable' => true,
190 ) );
191
192 $cmb_demo->add_field( array(
193 'name' => esc_html__( 'Test Text Email', 'cmb2' ),
194 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
195 'id' => $prefix . 'email',
196 'type' => 'text_email',
197 // 'repeatable' => true,
198 ) );
199
200 $cmb_demo->add_field( array(
201 'name' => esc_html__( 'Test Time', 'cmb2' ),
202 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
203 'id' => $prefix . 'time',
204 'type' => 'text_time',
205 // 'time_format' => 'H:i', // Set to 24hr format
206 ) );
207
208 $cmb_demo->add_field( array(
209 'name' => esc_html__( 'Time zone', 'cmb2' ),
210 'desc' => esc_html__( 'Time zone', 'cmb2' ),
211 'id' => $prefix . 'timezone',
212 'type' => 'select_timezone',
213 ) );
214
215 $cmb_demo->add_field( array(
216 'name' => esc_html__( 'Test Date Picker', 'cmb2' ),
217 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
218 'id' => $prefix . 'textdate',
219 'type' => 'text_date',
220 // 'date_format' => 'Y-m-d',
221 ) );
222
223 $cmb_demo->add_field( array(
224 'name' => esc_html__( 'Test Date Picker (UNIX timestamp)', 'cmb2' ),
225 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
226 'id' => $prefix . 'textdate_timestamp',
227 'type' => 'text_date_timestamp',
228 // 'timezone_meta_key' => $prefix . 'timezone', // Optionally make this field honor the timezone selected in the select_timezone specified above
229 ) );
230
231 $cmb_demo->add_field( array(
232 'name' => esc_html__( 'Test Date/Time Picker Combo (UNIX timestamp)', 'cmb2' ),
233 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
234 'id' => $prefix . 'datetime_timestamp',
235 'type' => 'text_datetime_timestamp',
236 ) );
237
238 // This text_datetime_timestamp_timezone field type
239 // is only compatible with PHP versions 5.3 or above.
240 // Feel free to uncomment and use if your server meets the requirement
241 // $cmb_demo->add_field( array(
242 // 'name' => esc_html__( 'Test Date/Time Picker/Time zone Combo (serialized DateTime object)', 'cmb2' ),
243 // 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
244 // 'id' => $prefix . 'datetime_timestamp_timezone',
245 // 'type' => 'text_datetime_timestamp_timezone',
246 // ) );
247
248 $cmb_demo->add_field( array(
249 'name' => esc_html__( 'Test Money', 'cmb2' ),
250 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
251 'id' => $prefix . 'textmoney',
252 'type' => 'text_money',
253 // 'before_field' => '£', // override '$' symbol if needed
254 // 'repeatable' => true,
255 ) );
256
257 $cmb_demo->add_field( array(
258 'name' => esc_html__( 'Test Color Picker', 'cmb2' ),
259 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
260 'id' => $prefix . 'colorpicker',
261 'type' => 'colorpicker',
262 'default' => '#ffffff',
263 // 'options' => array(
264 // 'alpha' => true, // Make this a rgba color picker.
265 // ),
266 // 'attributes' => array(
267 // 'data-colorpicker' => json_encode( array(
268 // 'palettes' => array( '#3dd0cc', '#ff834c', '#4fa2c0', '#0bc991', ),
269 // ) ),
270 // ),
271 ) );
272
273 $cmb_demo->add_field( array(
274 'name' => esc_html__( 'Test Text Area', 'cmb2' ),
275 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
276 'id' => $prefix . 'textarea',
277 'type' => 'textarea',
278 ) );
279
280 $cmb_demo->add_field( array(
281 'name' => esc_html__( 'Test Text Area Small', 'cmb2' ),
282 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
283 'id' => $prefix . 'textareasmall',
284 'type' => 'textarea_small',
285 ) );
286
287 $cmb_demo->add_field( array(
288 'name' => esc_html__( 'Test Text Area for Code', 'cmb2' ),
289 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
290 'id' => $prefix . 'textarea_code',
291 'type' => 'textarea_code',
292 ) );
293
294 $cmb_demo->add_field( array(
295 'name' => esc_html__( 'Test Title Weeeee', 'cmb2' ),
296 'desc' => esc_html__( 'This is a title description', 'cmb2' ),
297 'id' => $prefix . 'title',
298 'type' => 'title',
299 ) );
300
301 $cmb_demo->add_field( array(
302 'name' => esc_html__( 'Test Select', 'cmb2' ),
303 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
304 'id' => $prefix . 'select',
305 'type' => 'select',
306 'show_option_none' => true,
307 'options' => array(
308 'standard' => esc_html__( 'Option One', 'cmb2' ),
309 'custom' => esc_html__( 'Option Two', 'cmb2' ),
310 'none' => esc_html__( 'Option Three', 'cmb2' ),
311 ),
312 ) );
313
314 $cmb_demo->add_field( array(
315 'name' => esc_html__( 'Test Radio inline', 'cmb2' ),
316 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
317 'id' => $prefix . 'radio_inline',
318 'type' => 'radio_inline',
319 'show_option_none' => 'No Selection',
320 'options' => array(
321 'standard' => esc_html__( 'Option One', 'cmb2' ),
322 'custom' => esc_html__( 'Option Two', 'cmb2' ),
323 'none' => esc_html__( 'Option Three', 'cmb2' ),
324 ),
325 ) );
326
327 $cmb_demo->add_field( array(
328 'name' => esc_html__( 'Test Radio', 'cmb2' ),
329 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
330 'id' => $prefix . 'radio',
331 'type' => 'radio',
332 'options' => array(
333 'option1' => esc_html__( 'Option One', 'cmb2' ),
334 'option2' => esc_html__( 'Option Two', 'cmb2' ),
335 'option3' => esc_html__( 'Option Three', 'cmb2' ),
336 ),
337 ) );
338
339 $cmb_demo->add_field( array(
340 'name' => esc_html__( 'Test Taxonomy Radio', 'cmb2' ),
341 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
342 'id' => $prefix . 'text_taxonomy_radio',
343 'type' => 'taxonomy_radio',
344 'taxonomy' => 'category', // Taxonomy Slug
345 // 'inline' => true, // Toggles display to inline
346 ) );
347
348 $cmb_demo->add_field( array(
349 'name' => esc_html__( 'Test Taxonomy Select', 'cmb2' ),
350 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
351 'id' => $prefix . 'taxonomy_select',
352 'type' => 'taxonomy_select',
353 'taxonomy' => 'category', // Taxonomy Slug
354 ) );
355
356 $cmb_demo->add_field( array(
357 'name' => esc_html__( 'Test Taxonomy Multi Checkbox', 'cmb2' ),
358 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
359 'id' => $prefix . 'multitaxonomy',
360 'type' => 'taxonomy_multicheck',
361 'taxonomy' => 'post_tag', // Taxonomy Slug
362 // 'inline' => true, // Toggles display to inline
363 ) );
364
365 $cmb_demo->add_field( array(
366 'name' => esc_html__( 'Test Checkbox', 'cmb2' ),
367 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
368 'id' => $prefix . 'checkbox',
369 'type' => 'checkbox',
370 ) );
371
372 $cmb_demo->add_field( array(
373 'name' => esc_html__( 'Test Multi Checkbox', 'cmb2' ),
374 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
375 'id' => $prefix . 'multicheckbox',
376 'type' => 'multicheck',
377 // 'multiple' => true, // Store values in individual rows
378 'options' => array(
379 'check1' => esc_html__( 'Check One', 'cmb2' ),
380 'check2' => esc_html__( 'Check Two', 'cmb2' ),
381 'check3' => esc_html__( 'Check Three', 'cmb2' ),
382 ),
383 // 'inline' => true, // Toggles display to inline
384 ) );
385
386 $cmb_demo->add_field( array(
387 'name' => esc_html__( 'Test wysiwyg', 'cmb2' ),
388 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
389 'id' => $prefix . 'wysiwyg',
390 'type' => 'wysiwyg',
391 'options' => array(
392 'textarea_rows' => 5,
393 ),
394 ) );
395
396 $cmb_demo->add_field( array(
397 'name' => esc_html__( 'Test Image', 'cmb2' ),
398 'desc' => esc_html__( 'Upload an image or enter a URL.', 'cmb2' ),
399 'id' => $prefix . 'image',
400 'type' => 'file',
401 ) );
402
403 $cmb_demo->add_field( array(
404 'name' => esc_html__( 'Multiple Files', 'cmb2' ),
405 'desc' => esc_html__( 'Upload or add multiple images/attachments.', 'cmb2' ),
406 'id' => $prefix . 'file_list',
407 'type' => 'file_list',
408 'preview_size' => array( 100, 100 ), // Default: array( 50, 50 )
409 ) );
410
411 $cmb_demo->add_field( array(
412 'name' => esc_html__( 'oEmbed', 'cmb2' ),
413 'desc' => sprintf(
414 /* translators: %s: link to codex.wordpress.org/Embeds */
415 esc_html__( 'Enter a youtube, twitter, or instagram URL. Supports services listed at %s.', 'cmb2' ),
416 '<a href="https://codex.wordpress.org/Embeds">codex.wordpress.org/Embeds</a>'
417 ),
418 'id' => $prefix . 'embed',
419 'type' => 'oembed',
420 ) );
421
422 $cmb_demo->add_field( array(
423 'name' => 'Testing Field Parameters',
424 'id' => $prefix . 'parameters',
425 'type' => 'text',
426 'before_row' => 'yourprefix_before_row_if_2', // callback.
427 'before' => '<p>Testing <b>"before"</b> parameter</p>',
428 'before_field' => '<p>Testing <b>"before_field"</b> parameter</p>',
429 'after_field' => '<p>Testing <b>"after_field"</b> parameter</p>',
430 'after' => '<p>Testing <b>"after"</b> parameter</p>',
431 'after_row' => '<p>Testing <b>"after_row"</b> parameter</p>',
432 ) );
433
434 }
435
436 add_action( 'cmb2_admin_init', 'yourprefix_register_about_page_metabox' );
437 /**
438 * Hook in and add a metabox that only appears on the 'About' page
439 */
440 function yourprefix_register_about_page_metabox() {
441 $prefix = 'yourprefix_about_';
442
443 /**
444 * Metabox to be displayed on a single page ID
445 */
446 $cmb_about_page = new_cmb2_box( array(
447 'id' => $prefix . 'metabox',
448 'title' => esc_html__( 'About Page Metabox', 'cmb2' ),
449 'object_types' => array( 'page' ), // Post type
450 'context' => 'normal',
451 'priority' => 'high',
452 'show_names' => true, // Show field names on the left
453 'show_on' => array(
454 'id' => array( 2 ),
455 ), // Specific post IDs to display this metabox
456 ) );
457
458 $cmb_about_page->add_field( array(
459 'name' => esc_html__( 'Test Text', 'cmb2' ),
460 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
461 'id' => $prefix . 'text',
462 'type' => 'text',
463 ) );
464
465 }
466
467 add_action( 'cmb2_admin_init', 'yourprefix_register_repeatable_group_field_metabox' );
468 /**
469 * Hook in and add a metabox to demonstrate repeatable grouped fields
470 */
471 function yourprefix_register_repeatable_group_field_metabox() {
472 $prefix = 'yourprefix_group_';
473
474 /**
475 * Repeatable Field Groups
476 */
477 $cmb_group = new_cmb2_box( array(
478 'id' => $prefix . 'metabox',
479 'title' => esc_html__( 'Repeating Field Group', 'cmb2' ),
480 'object_types' => array( 'page' ),
481 ) );
482
483 // $group_field_id is the field id string, so in this case: $prefix . 'demo'
484 $group_field_id = $cmb_group->add_field( array(
485 'id' => $prefix . 'demo',
486 'type' => 'group',
487 'description' => esc_html__( 'Generates reusable form entries', 'cmb2' ),
488 'options' => array(
489 'group_title' => esc_html__( 'Entry {#}', 'cmb2' ), // {#} gets replaced by row number
490 'add_button' => esc_html__( 'Add Another Entry', 'cmb2' ),
491 'remove_button' => esc_html__( 'Remove Entry', 'cmb2' ),
492 'sortable' => true, // beta
493 // 'closed' => true, // true to have the groups closed by default
494 ),
495 ) );
496
497 /**
498 * Group fields works the same, except ids only need
499 * to be unique to the group. Prefix is not needed.
500 *
501 * The parent field's id needs to be passed as the first argument.
502 */
503 $cmb_group->add_group_field( $group_field_id, array(
504 'name' => esc_html__( 'Entry Title', 'cmb2' ),
505 'id' => 'title',
506 'type' => 'text',
507 // 'repeatable' => true, // Repeatable fields are supported w/in repeatable groups (for most types)
508 ) );
509
510 $cmb_group->add_group_field( $group_field_id, array(
511 'name' => esc_html__( 'Description', 'cmb2' ),
512 'description' => esc_html__( 'Write a short description for this entry', 'cmb2' ),
513 'id' => 'description',
514 'type' => 'textarea_small',
515 ) );
516
517 $cmb_group->add_group_field( $group_field_id, array(
518 'name' => esc_html__( 'Entry Image', 'cmb2' ),
519 'id' => 'image',
520 'type' => 'file',
521 ) );
522
523 $cmb_group->add_group_field( $group_field_id, array(
524 'name' => esc_html__( 'Image Caption', 'cmb2' ),
525 'id' => 'image_caption',
526 'type' => 'text',
527 ) );
528
529 }
530
531 add_action( 'cmb2_admin_init', 'yourprefix_register_user_profile_metabox' );
532 /**
533 * Hook in and add a metabox to add fields to the user profile pages
534 */
535 function yourprefix_register_user_profile_metabox() {
536 $prefix = 'yourprefix_user_';
537
538 /**
539 * Metabox for the user profile screen
540 */
541 $cmb_user = new_cmb2_box( array(
542 'id' => $prefix . 'edit',
543 'title' => esc_html__( 'User Profile Metabox', 'cmb2' ), // Doesn't output for user boxes
544 'object_types' => array( 'user' ), // Tells CMB2 to use user_meta vs post_meta
545 'show_names' => true,
546 'new_user_section' => 'add-new-user', // where form will show on new user page. 'add-existing-user' is only other valid option.
547 ) );
548
549 $cmb_user->add_field( array(
550 'name' => esc_html__( 'Extra Info', 'cmb2' ),
551 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
552 'id' => $prefix . 'extra_info',
553 'type' => 'title',
554 'on_front' => false,
555 ) );
556
557 $cmb_user->add_field( array(
558 'name' => esc_html__( 'Avatar', 'cmb2' ),
559 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
560 'id' => $prefix . 'avatar',
561 'type' => 'file',
562 ) );
563
564 $cmb_user->add_field( array(
565 'name' => esc_html__( 'Facebook URL', 'cmb2' ),
566 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
567 'id' => $prefix . 'facebookurl',
568 'type' => 'text_url',
569 ) );
570
571 $cmb_user->add_field( array(
572 'name' => esc_html__( 'Twitter URL', 'cmb2' ),
573 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
574 'id' => $prefix . 'twitterurl',
575 'type' => 'text_url',
576 ) );
577
578 $cmb_user->add_field( array(
579 'name' => esc_html__( 'Google+ URL', 'cmb2' ),
580 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
581 'id' => $prefix . 'googleplusurl',
582 'type' => 'text_url',
583 ) );
584
585 $cmb_user->add_field( array(
586 'name' => esc_html__( 'Linkedin URL', 'cmb2' ),
587 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
588 'id' => $prefix . 'linkedinurl',
589 'type' => 'text_url',
590 ) );
591
592 $cmb_user->add_field( array(
593 'name' => esc_html__( 'User Field', 'cmb2' ),
594 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
595 'id' => $prefix . 'user_text_field',
596 'type' => 'text',
597 ) );
598
599 }
600
601 add_action( 'cmb2_admin_init', 'yourprefix_register_taxonomy_metabox' );
602 /**
603 * Hook in and add a metabox to add fields to taxonomy terms
604 */
605 function yourprefix_register_taxonomy_metabox() {
606 $prefix = 'yourprefix_term_';
607
608 /**
609 * Metabox to add fields to categories and tags
610 */
611 $cmb_term = new_cmb2_box( array(
612 'id' => $prefix . 'edit',
613 'title' => esc_html__( 'Category Metabox', 'cmb2' ), // Doesn't output for term boxes
614 'object_types' => array( 'term' ), // Tells CMB2 to use term_meta vs post_meta
615 'taxonomies' => array( 'category', 'post_tag' ), // Tells CMB2 which taxonomies should have these fields
616 // 'new_term_section' => true, // Will display in the "Add New Category" section
617 ) );
618
619 $cmb_term->add_field( array(
620 'name' => esc_html__( 'Extra Info', 'cmb2' ),
621 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
622 'id' => $prefix . 'extra_info',
623 'type' => 'title',
624 'on_front' => false,
625 ) );
626
627 $cmb_term->add_field( array(
628 'name' => esc_html__( 'Term Image', 'cmb2' ),
629 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
630 'id' => $prefix . 'avatar',
631 'type' => 'file',
632 ) );
633
634 $cmb_term->add_field( array(
635 'name' => esc_html__( 'Arbitrary Term Field', 'cmb2' ),
636 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
637 'id' => $prefix . 'term_text_field',
638 'type' => 'text',
639 ) );
640
641 }
642
643 add_action( 'cmb2_admin_init', 'yourprefix_register_theme_options_metabox' );
644 /**
645 * Hook in and register a metabox to handle a theme options page and adds a menu item.
646 */
647 function yourprefix_register_theme_options_metabox() {
648
649 /**
650 * Registers options page menu item and form.
651 */
652 $cmb_options = new_cmb2_box( array(
653 'id' => 'yourprefix_theme_options_page',
654 'title' => esc_html__( 'Theme Options', 'cmb2' ),
655 'object_types' => array( 'options-page' ),
656
657 /*
658 * The following parameters are specific to the options-page box
659 * Several of these parameters are passed along to add_menu_page()/add_submenu_page().
660 */
661
662 'option_key' => 'yourprefix_theme_options', // The option key and admin menu page slug.
663 'icon_url' => 'dashicons-palmtree', // Menu icon. Only applicable if 'parent_slug' is left empty.
664 // 'menu_title' => esc_html__( 'Options', 'cmb2' ), // Falls back to 'title' (above).
665 // 'parent_slug' => 'themes.php', // Make options page a submenu item of the themes menu.
666 // 'capability' => 'manage_options', // Cap required to view options-page.
667 // 'position' => 1, // Menu position. Only applicable if 'parent_slug' is left empty.
668 // 'admin_menu_hook' => 'network_admin_menu', // 'network_admin_menu' to add network-level options page.
669 // 'display_cb' => false, // Override the options-page form output (CMB2_Hookup::options_page_output()).
670 // 'save_button' => esc_html__( 'Save Theme Options', 'cmb2' ), // The text for the options-page save button. Defaults to 'Save'.
671 // 'disable_settings_errors' => true, // On settings pages (not options-general.php sub-pages), allows disabling.
672 // 'message_cb' => 'yourprefix_options_page_message_callback',
673 ) );
674
675 /**
676 * Options fields ids only need
677 * to be unique within this box.
678 * Prefix is not needed.
679 */
680 $cmb_options->add_field( array(
681 'name' => esc_html__( 'Site Background Color', 'cmb2' ),
682 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
683 'id' => 'bg_color',
684 'type' => 'colorpicker',
685 'default' => '#ffffff',
686 ) );
687
688 }
689
690 /**
691 * Callback to define the optionss-saved message.
692 *
693 * @param CMB2 $cmb The CMB2 object.
694 * @param array $args {
695 * An array of message arguments
696 *
697 * @type bool $is_options_page Whether current page is this options page.
698 * @type bool $should_notify Whether options were saved and we should be notified.
699 * @type bool $is_updated Whether options were updated with save (or stayed the same).
700 * @type string $setting For add_settings_error(), Slug title of the setting to which
701 * this error applies.
702 * @type string $code For add_settings_error(), Slug-name to identify the error.
703 * Used as part of 'id' attribute in HTML output.
704 * @type string $message For add_settings_error(), The formatted message text to display
705 * to the user (will be shown inside styled `<div>` and `<p>` tags).
706 * Will be 'Settings updated.' if $is_updated is true, else 'Nothing to update.'
707 * @type string $type For add_settings_error(), Message type, controls HTML class.
708 * Accepts 'error', 'updated', '', 'notice-warning', etc.
709 * Will be 'updated' if $is_updated is true, else 'notice-warning'.
710 * }
711 */
712 function yourprefix_options_page_message_callback( $cmb, $args ) {
713 if ( ! empty( $args['should_notify'] ) ) {
714
715 if ( $args['is_updated'] ) {
716
717 // Modify the updated message.
718 $args['message'] = sprintf( esc_html__( '%s &mdash; Updated!', 'cmb2' ), $cmb->prop( 'title' ) );
719 }
720
721 add_settings_error( $args['setting'], $args['code'], $args['message'], $args['type'] );
722 }
723 }
724
725 /**
726 * Only show this box in the CMB2 REST API if the user is logged in.
727 *
728 * @param bool $is_allowed Whether this box and its fields are allowed to be viewed.
729 * @param CMB2_REST_Controller $cmb_controller The controller object.
730 * CMB2 object available via `$cmb_controller->rest_box->cmb`.
731 *
732 * @return bool Whether this box and its fields are allowed to be viewed.
733 */
734 function yourprefix_limit_rest_view_to_logged_in_users( $is_allowed, $cmb_controller ) {
735 if ( ! is_user_logged_in() ) {
736 $is_allowed = false;
737 }
738
739 return $is_allowed;
740 }
741
742 add_action( 'cmb2_init', 'yourprefix_register_rest_api_box' );
743 /**
744 * Hook in and add a box to be available in the CMB2 REST API. Can only happen on the 'cmb2_init' hook.
745 * More info: https://github.com/CMB2/CMB2/wiki/REST-API
746 */
747 function yourprefix_register_rest_api_box() {
748 $prefix = 'yourprefix_rest_';
749
750 $cmb_rest = new_cmb2_box( array(
751 'id' => $prefix . 'metabox',
752 'title' => esc_html__( 'REST Test Box', 'cmb2' ),
753 'object_types' => array( 'page' ), // Post type
754 'show_in_rest' => WP_REST_Server::ALLMETHODS, // WP_REST_Server::READABLE|WP_REST_Server::EDITABLE, // Determines which HTTP methods the box is visible in.
755 // Optional callback to limit box visibility.
756 // See: https://github.com/CMB2/CMB2/wiki/REST-API#permissions
757 // 'get_box_permissions_check_cb' => 'yourprefix_limit_rest_view_to_logged_in_users',
758 ) );
759
760 $cmb_rest->add_field( array(
761 'name' => esc_html__( 'REST Test Text', 'cmb2' ),
762 'desc' => esc_html__( 'Will show in the REST API for this box and for pages.', 'cmb2' ),
763 'id' => $prefix . 'text',
764 'type' => 'text',
765 ) );
766
767 $cmb_rest->add_field( array(
768 'name' => esc_html__( 'REST Editable Test Text', 'cmb2' ),
769 'desc' => esc_html__( 'Will show in REST API "editable" contexts only (`POST` requests).', 'cmb2' ),
770 'id' => $prefix . 'editable_text',
771 'type' => 'text',
772 'show_in_rest' => WP_REST_Server::EDITABLE,// WP_REST_Server::ALLMETHODS|WP_REST_Server::READABLE, // Determines which HTTP methods the field is visible in. Will override the cmb2_box 'show_in_rest' param.
773 ) );
774 }
775