PluginProbe
WPGet API – Connect to any external REST API / 2.2.2
WPGet API – Connect to any external REST API v2.2.2
1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.3 2.1.4 2.1.5 2.2.0 2.2.1 2.2.10 2.2.2 2.2.3 All 97 releases
wpgetapi / lib / cmb2 / example-functions.php

example-functions.php in WPGet API – Connect to any external REST API 2.2.2, at lib/cmb2/example-functions.php

808 lines 30.0 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 /**
111 * Sample metabox to demonstrate each field type included
112 */
113 $cmb_demo = new_cmb2_box( array(
114 'id' => 'yourprefix_demo_metabox',
115 'title' => esc_html__( 'Test Metabox', 'cmb2' ),
116 'object_types' => array( 'page' ), // Post type
117 // 'show_on_cb' => 'yourprefix_show_if_front_page', // function should return a bool value
118 // 'context' => 'normal',
119 // 'priority' => 'high',
120 // 'show_names' => true, // Show field names on the left
121 // 'cmb_styles' => false, // false to disable the CMB stylesheet
122 // 'closed' => true, // true to keep the metabox closed by default
123 // 'classes' => 'extra-class', // Extra cmb2-wrap classes
124 // 'classes_cb' => 'yourprefix_add_some_classes', // Add classes through a callback.
125
126 /*
127 * The following parameter is any additional arguments passed as $callback_args
128 * to add_meta_box, if/when applicable.
129 *
130 * CMB2 does not use these arguments in the add_meta_box callback, however, these args
131 * are parsed for certain special properties, like determining Gutenberg/block-editor
132 * compatibility.
133 *
134 * Examples:
135 *
136 * - Make sure default editor is used as metabox is not compatible with block editor
137 * [ '__block_editor_compatible_meta_box' => false/true ]
138 *
139 * - Or declare this box exists for backwards compatibility
140 * [ '__back_compat_meta_box' => false ]
141 *
142 * More: https://wordpress.org/gutenberg/handbook/extensibility/meta-box/
143 */
144 // 'mb_callback_args' => array( '__block_editor_compatible_meta_box' => false ),
145 ) );
146
147 $cmb_demo->add_field( array(
148 'name' => esc_html__( 'Test Text', 'cmb2' ),
149 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
150 'id' => 'yourprefix_demo_text',
151 'type' => 'text',
152 'show_on_cb' => 'yourprefix_hide_if_no_cats', // function should return a bool value
153 // 'sanitization_cb' => 'my_custom_sanitization', // custom sanitization callback parameter
154 // 'escape_cb' => 'my_custom_escaping', // custom escaping callback parameter
155 // 'on_front' => false, // Optionally designate a field to wp-admin only
156 // 'repeatable' => true,
157 // 'column' => true, // Display field value in the admin post-listing columns
158 ) );
159
160 $cmb_demo->add_field( array(
161 'name' => esc_html__( 'Test Text Small', 'cmb2' ),
162 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
163 'id' => 'yourprefix_demo_textsmall',
164 'type' => 'text_small',
165 // 'repeatable' => true,
166 // 'column' => array(
167 // 'name' => esc_html__( 'Column Title', 'cmb2' ), // Set the admin column title
168 // 'position' => 2, // Set as the second column.
169 // );
170 // 'display_cb' => 'yourprefix_display_text_small_column', // Output the display of the column values through a callback.
171 ) );
172
173 $cmb_demo->add_field( array(
174 'name' => esc_html__( 'Test Text Medium', 'cmb2' ),
175 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
176 'id' => 'yourprefix_demo_textmedium',
177 'type' => 'text_medium',
178 ) );
179
180 $cmb_demo->add_field( array(
181 'name' => esc_html__( 'Read-only Disabled Field', 'cmb2' ),
182 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
183 'id' => 'yourprefix_demo_readonly',
184 'type' => 'text_medium',
185 'default' => esc_attr__( 'Hey there, I\'m a read-only field', 'cmb2' ),
186 'save_field' => false, // Disables the saving of this field.
187 'attributes' => array(
188 'disabled' => 'disabled',
189 'readonly' => 'readonly',
190 ),
191 ) );
192
193 $cmb_demo->add_field( array(
194 'name' => esc_html__( 'Custom Rendered Field', 'cmb2' ),
195 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
196 'id' => 'yourprefix_demo_render_row_cb',
197 'type' => 'text',
198 'render_row_cb' => 'yourprefix_render_row_cb',
199 ) );
200
201 $cmb_demo->add_field( array(
202 'name' => esc_html__( 'Website URL', 'cmb2' ),
203 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
204 'id' => 'yourprefix_demo_url',
205 'type' => 'text_url',
206 // 'protocols' => array('http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet'), // Array of allowed protocols
207 // 'repeatable' => true,
208 ) );
209
210 $cmb_demo->add_field( array(
211 'name' => esc_html__( 'Test Text Email', 'cmb2' ),
212 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
213 'id' => 'yourprefix_demo_email',
214 'type' => 'text_email',
215 // 'repeatable' => true,
216 ) );
217
218 $cmb_demo->add_field( array(
219 'name' => esc_html__( 'Test Time', 'cmb2' ),
220 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
221 'id' => 'yourprefix_demo_time',
222 'type' => 'text_time',
223 // 'time_format' => 'H:i', // Set to 24hr format
224 ) );
225
226 $cmb_demo->add_field( array(
227 'name' => esc_html__( 'Time zone', 'cmb2' ),
228 'desc' => esc_html__( 'Time zone', 'cmb2' ),
229 'id' => 'yourprefix_demo_timezone',
230 'type' => 'select_timezone',
231 ) );
232
233 $cmb_demo->add_field( array(
234 'name' => esc_html__( 'Test Date Picker', 'cmb2' ),
235 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
236 'id' => 'yourprefix_demo_textdate',
237 'type' => 'text_date',
238 // 'date_format' => 'Y-m-d',
239 ) );
240
241 $cmb_demo->add_field( array(
242 'name' => esc_html__( 'Test Date Picker (UNIX timestamp)', 'cmb2' ),
243 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
244 'id' => 'yourprefix_demo_textdate_timestamp',
245 'type' => 'text_date_timestamp',
246 // 'timezone_meta_key' => 'yourprefix_demo_timezone', // Optionally make this field honor the timezone selected in the select_timezone specified above
247 ) );
248
249 $cmb_demo->add_field( array(
250 'name' => esc_html__( 'Test Date/Time Picker Combo (UNIX timestamp)', 'cmb2' ),
251 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
252 'id' => 'yourprefix_demo_datetime_timestamp',
253 'type' => 'text_datetime_timestamp',
254 ) );
255
256 // This text_datetime_timestamp_timezone field type
257 // is only compatible with PHP versions 5.3 or above.
258 // Feel free to uncomment and use if your server meets the requirement
259 // $cmb_demo->add_field( array(
260 // 'name' => esc_html__( 'Test Date/Time Picker/Time zone Combo (serialized DateTime object)', 'cmb2' ),
261 // 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
262 // 'id' => 'yourprefix_demo_datetime_timestamp_timezone',
263 // 'type' => 'text_datetime_timestamp_timezone',
264 // ) );
265
266 $cmb_demo->add_field( array(
267 'name' => esc_html__( 'Test Money', 'cmb2' ),
268 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
269 'id' => 'yourprefix_demo_textmoney',
270 'type' => 'text_money',
271 // 'before_field' => '£', // override '$' symbol if needed
272 // 'repeatable' => true,
273 ) );
274
275 $cmb_demo->add_field( array(
276 'name' => esc_html__( 'Test Color Picker', 'cmb2' ),
277 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
278 'id' => 'yourprefix_demo_colorpicker',
279 'type' => 'colorpicker',
280 'default' => '#ffffff',
281 // 'options' => array(
282 // 'alpha' => true, // Make this a rgba color picker.
283 // ),
284 // 'attributes' => array(
285 // 'data-colorpicker' => json_encode( array(
286 // 'palettes' => array( '#3dd0cc', '#ff834c', '#4fa2c0', '#0bc991', ),
287 // ) ),
288 // ),
289 ) );
290
291 $cmb_demo->add_field( array(
292 'name' => esc_html__( 'Test Text Area', 'cmb2' ),
293 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
294 'id' => 'yourprefix_demo_textarea',
295 'type' => 'textarea',
296 ) );
297
298 $cmb_demo->add_field( array(
299 'name' => esc_html__( 'Test Text Area Small', 'cmb2' ),
300 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
301 'id' => 'yourprefix_demo_textareasmall',
302 'type' => 'textarea_small',
303 ) );
304
305 $cmb_demo->add_field( array(
306 'name' => esc_html__( 'Test Text Area for Code', 'cmb2' ),
307 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
308 'id' => 'yourprefix_demo_textarea_code',
309 'type' => 'textarea_code',
310 // 'attributes' => array(
311 // // Optionally override the code editor defaults.
312 // 'data-codeeditor' => json_encode( array(
313 // 'codemirror' => array(
314 // 'lineNumbers' => false,
315 // 'mode' => 'css',
316 // ),
317 // ) ),
318 // ),
319 // To keep the previous formatting, you can disable codemirror.
320 // 'options' => array( 'disable_codemirror' => true ),
321 ) );
322
323 $cmb_demo->add_field( array(
324 'name' => esc_html__( 'Test Title Weeeee', 'cmb2' ),
325 'desc' => esc_html__( 'This is a title description', 'cmb2' ),
326 'id' => 'yourprefix_demo_title',
327 'type' => 'title',
328 ) );
329
330 $cmb_demo->add_field( array(
331 'name' => esc_html__( 'Test Select', 'cmb2' ),
332 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
333 'id' => 'yourprefix_demo_select',
334 'type' => 'select',
335 'show_option_none' => true,
336 'options' => array(
337 'standard' => esc_html__( 'Option One', 'cmb2' ),
338 'custom' => esc_html__( 'Option Two', 'cmb2' ),
339 'none' => esc_html__( 'Option Three', 'cmb2' ),
340 ),
341 ) );
342
343 $cmb_demo->add_field( array(
344 'name' => esc_html__( 'Test Radio inline', 'cmb2' ),
345 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
346 'id' => 'yourprefix_demo_radio_inline',
347 'type' => 'radio_inline',
348 'show_option_none' => 'No Selection',
349 'options' => array(
350 'standard' => esc_html__( 'Option One', 'cmb2' ),
351 'custom' => esc_html__( 'Option Two', 'cmb2' ),
352 'none' => esc_html__( 'Option Three', 'cmb2' ),
353 ),
354 ) );
355
356 $cmb_demo->add_field( array(
357 'name' => esc_html__( 'Test Radio', 'cmb2' ),
358 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
359 'id' => 'yourprefix_demo_radio',
360 'type' => 'radio',
361 'options' => array(
362 'option1' => esc_html__( 'Option One', 'cmb2' ),
363 'option2' => esc_html__( 'Option Two', 'cmb2' ),
364 'option3' => esc_html__( 'Option Three', 'cmb2' ),
365 ),
366 ) );
367
368 $cmb_demo->add_field( array(
369 'name' => esc_html__( 'Test Taxonomy Radio', 'cmb2' ),
370 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
371 'id' => 'yourprefix_demo_text_taxonomy_radio',
372 'type' => 'taxonomy_radio', // Or `taxonomy_radio_inline`/`taxonomy_radio_hierarchical`
373 'taxonomy' => 'category', // Taxonomy Slug
374 // 'inline' => true, // Toggles display to inline
375 // Optionally override the args sent to the WordPress get_terms function.
376 'query_args' => array(
377 // 'orderby' => 'slug',
378 // 'hide_empty' => true,
379 ),
380 ) );
381
382 $cmb_demo->add_field( array(
383 'name' => esc_html__( 'Test Taxonomy Select', 'cmb2' ),
384 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
385 'id' => 'yourprefix_demo_taxonomy_select',
386 'type' => 'taxonomy_select', // Or `taxonomy_select_hierarchical`
387 'taxonomy' => 'category', // Taxonomy Slug
388 ) );
389
390 $cmb_demo->add_field( array(
391 'name' => esc_html__( 'Test Taxonomy Multi Checkbox', 'cmb2' ),
392 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
393 'id' => 'yourprefix_demo_multitaxonomy',
394 'type' => 'taxonomy_multicheck', // Or `taxonomy_multicheck_inline`/`taxonomy_multicheck_hierarchical`
395 'taxonomy' => 'post_tag', // Taxonomy Slug
396 // 'inline' => true, // Toggles display to inline
397 ) );
398
399 $cmb_demo->add_field( array(
400 'name' => esc_html__( 'Test Checkbox', 'cmb2' ),
401 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
402 'id' => 'yourprefix_demo_checkbox',
403 'type' => 'checkbox',
404 ) );
405
406 $cmb_demo->add_field( array(
407 'name' => esc_html__( 'Test Multi Checkbox', 'cmb2' ),
408 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
409 'id' => 'yourprefix_demo_multicheckbox',
410 'type' => 'multicheck',
411 // 'multiple' => true, // Store values in individual rows
412 'options' => array(
413 'check1' => esc_html__( 'Check One', 'cmb2' ),
414 'check2' => esc_html__( 'Check Two', 'cmb2' ),
415 'check3' => esc_html__( 'Check Three', 'cmb2' ),
416 ),
417 // 'inline' => true, // Toggles display to inline
418 ) );
419
420 $cmb_demo->add_field( array(
421 'name' => esc_html__( 'Test wysiwyg', 'cmb2' ),
422 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
423 'id' => 'yourprefix_demo_wysiwyg',
424 'type' => 'wysiwyg',
425 'options' => array(
426 'textarea_rows' => 5,
427 ),
428 ) );
429
430 $cmb_demo->add_field( array(
431 'name' => esc_html__( 'Test Image', 'cmb2' ),
432 'desc' => esc_html__( 'Upload an image or enter a URL.', 'cmb2' ),
433 'id' => 'yourprefix_demo_image',
434 'type' => 'file',
435 ) );
436
437 $cmb_demo->add_field( array(
438 'name' => esc_html__( 'Multiple Files', 'cmb2' ),
439 'desc' => esc_html__( 'Upload or add multiple images/attachments.', 'cmb2' ),
440 'id' => 'yourprefix_demo_file_list',
441 'type' => 'file_list',
442 'preview_size' => array( 100, 100 ), // Default: array( 50, 50 )
443 ) );
444
445 $cmb_demo->add_field( array(
446 'name' => esc_html__( 'oEmbed', 'cmb2' ),
447 'desc' => sprintf(
448 /* translators: %s: link to codex.wordpress.org/Embeds */
449 esc_html__( 'Enter a youtube, twitter, or instagram URL. Supports services listed at %s.', 'cmb2' ),
450 '<a href="https://wordpress.org/support/article/embeds/">codex.wordpress.org/Embeds</a>'
451 ),
452 'id' => 'yourprefix_demo_embed',
453 'type' => 'oembed',
454 ) );
455
456 $cmb_demo->add_field( array(
457 'name' => 'Testing Field Parameters',
458 'id' => 'yourprefix_demo_parameters',
459 'type' => 'text',
460 'before_row' => 'yourprefix_before_row_if_2', // callback.
461 'before' => '<p>Testing <b>"before"</b> parameter</p>',
462 'before_field' => '<p>Testing <b>"before_field"</b> parameter</p>',
463 'after_field' => '<p>Testing <b>"after_field"</b> parameter</p>',
464 'after' => '<p>Testing <b>"after"</b> parameter</p>',
465 'after_row' => '<p>Testing <b>"after_row"</b> parameter</p>',
466 ) );
467
468 }
469
470 add_action( 'cmb2_admin_init', 'yourprefix_register_about_page_metabox' );
471 /**
472 * Hook in and add a metabox that only appears on the 'About' page
473 */
474 function yourprefix_register_about_page_metabox() {
475
476 /**
477 * Metabox to be displayed on a single page ID
478 */
479 $cmb_about_page = new_cmb2_box( array(
480 'id' => 'yourprefix_about_metabox',
481 'title' => esc_html__( 'About Page Metabox', 'cmb2' ),
482 'object_types' => array( 'page' ), // Post type
483 'context' => 'normal',
484 'priority' => 'high',
485 'show_names' => true, // Show field names on the left
486 'show_on' => array(
487 'id' => array( 2 ),
488 ), // Specific post IDs to display this metabox
489 ) );
490
491 $cmb_about_page->add_field( array(
492 'name' => esc_html__( 'Test Text', 'cmb2' ),
493 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
494 'id' => 'yourprefix_about_text',
495 'type' => 'text',
496 ) );
497
498 }
499
500 add_action( 'cmb2_admin_init', 'yourprefix_register_repeatable_group_field_metabox' );
501 /**
502 * Hook in and add a metabox to demonstrate repeatable grouped fields
503 */
504 function yourprefix_register_repeatable_group_field_metabox() {
505
506 /**
507 * Repeatable Field Groups
508 */
509 $cmb_group = new_cmb2_box( array(
510 'id' => 'yourprefix_group_metabox',
511 'title' => esc_html__( 'Repeating Field Group', 'cmb2' ),
512 'object_types' => array( 'page' ),
513 ) );
514
515 // $group_field_id is the field id string, so in this case: 'yourprefix_group_demo'
516 $group_field_id = $cmb_group->add_field( array(
517 'id' => 'yourprefix_group_demo',
518 'type' => 'group',
519 'description' => esc_html__( 'Generates reusable form entries', 'cmb2' ),
520 'options' => array(
521 'group_title' => esc_html__( 'Entry {#}', 'cmb2' ), // {#} gets replaced by row number
522 'add_button' => esc_html__( 'Add Another Entry', 'cmb2' ),
523 'remove_button' => esc_html__( 'Remove Entry', 'cmb2' ),
524 'sortable' => true,
525 // 'closed' => true, // true to have the groups closed by default
526 // 'remove_confirm' => esc_html__( 'Are you sure you want to remove?', 'cmb2' ), // Performs confirmation before removing group.
527 ),
528 ) );
529
530 /**
531 * Group fields works the same, except ids only need
532 * to be unique to the group. Prefix is not needed.
533 *
534 * The parent field's id needs to be passed as the first argument.
535 */
536 $cmb_group->add_group_field( $group_field_id, array(
537 'name' => esc_html__( 'Entry Title', 'cmb2' ),
538 'id' => 'title',
539 'type' => 'text',
540 // 'repeatable' => true, // Repeatable fields are supported w/in repeatable groups (for most types)
541 ) );
542
543 $cmb_group->add_group_field( $group_field_id, array(
544 'name' => esc_html__( 'Description', 'cmb2' ),
545 'description' => esc_html__( 'Write a short description for this entry', 'cmb2' ),
546 'id' => 'description',
547 'type' => 'textarea_small',
548 ) );
549
550 $cmb_group->add_group_field( $group_field_id, array(
551 'name' => esc_html__( 'Entry Image', 'cmb2' ),
552 'id' => 'image',
553 'type' => 'file',
554 ) );
555
556 $cmb_group->add_group_field( $group_field_id, array(
557 'name' => esc_html__( 'Image Caption', 'cmb2' ),
558 'id' => 'image_caption',
559 'type' => 'text',
560 ) );
561
562 }
563
564 add_action( 'cmb2_admin_init', 'yourprefix_register_user_profile_metabox' );
565 /**
566 * Hook in and add a metabox to add fields to the user profile pages
567 */
568 function yourprefix_register_user_profile_metabox() {
569
570 /**
571 * Metabox for the user profile screen
572 */
573 $cmb_user = new_cmb2_box( array(
574 'id' => 'yourprefix_user_edit',
575 'title' => esc_html__( 'User Profile Metabox', 'cmb2' ), // Doesn't output for user boxes
576 'object_types' => array( 'user' ), // Tells CMB2 to use user_meta vs post_meta
577 'show_names' => true,
578 'new_user_section' => 'add-new-user', // where form will show on new user page. 'add-existing-user' is only other valid option.
579 ) );
580
581 $cmb_user->add_field( array(
582 'name' => esc_html__( 'Extra Info', 'cmb2' ),
583 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
584 'id' => 'yourprefix_user_extra_info',
585 'type' => 'title',
586 'on_front' => false,
587 ) );
588
589 $cmb_user->add_field( array(
590 'name' => esc_html__( 'Avatar', 'cmb2' ),
591 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
592 'id' => 'yourprefix_user_avatar',
593 'type' => 'file',
594 ) );
595
596 $cmb_user->add_field( array(
597 'name' => esc_html__( 'Facebook URL', 'cmb2' ),
598 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
599 'id' => 'yourprefix_user_facebookurl',
600 'type' => 'text_url',
601 ) );
602
603 $cmb_user->add_field( array(
604 'name' => esc_html__( 'Twitter URL', 'cmb2' ),
605 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
606 'id' => 'yourprefix_user_twitterurl',
607 'type' => 'text_url',
608 ) );
609
610 $cmb_user->add_field( array(
611 'name' => esc_html__( 'Google+ URL', 'cmb2' ),
612 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
613 'id' => 'yourprefix_user_googleplusurl',
614 'type' => 'text_url',
615 ) );
616
617 $cmb_user->add_field( array(
618 'name' => esc_html__( 'Linkedin URL', 'cmb2' ),
619 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
620 'id' => 'yourprefix_user_linkedinurl',
621 'type' => 'text_url',
622 ) );
623
624 $cmb_user->add_field( array(
625 'name' => esc_html__( 'User Field', 'cmb2' ),
626 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
627 'id' => 'yourprefix_user_user_text_field',
628 'type' => 'text',
629 ) );
630
631 }
632
633 add_action( 'cmb2_admin_init', 'yourprefix_register_taxonomy_metabox' );
634 /**
635 * Hook in and add a metabox to add fields to taxonomy terms
636 */
637 function yourprefix_register_taxonomy_metabox() {
638
639 /**
640 * Metabox to add fields to categories and tags
641 */
642 $cmb_term = new_cmb2_box( array(
643 'id' => 'yourprefix_term_edit',
644 'title' => esc_html__( 'Category Metabox', 'cmb2' ), // Doesn't output for term boxes
645 'object_types' => array( 'term' ), // Tells CMB2 to use term_meta vs post_meta
646 'taxonomies' => array( 'category', 'post_tag' ), // Tells CMB2 which taxonomies should have these fields
647 // 'new_term_section' => true, // Will display in the "Add New Category" section
648 ) );
649
650 $cmb_term->add_field( array(
651 'name' => esc_html__( 'Extra Info', 'cmb2' ),
652 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
653 'id' => 'yourprefix_term_extra_info',
654 'type' => 'title',
655 'on_front' => false,
656 ) );
657
658 $cmb_term->add_field( array(
659 'name' => esc_html__( 'Term Image', 'cmb2' ),
660 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
661 'id' => 'yourprefix_term_avatar',
662 'type' => 'file',
663 ) );
664
665 $cmb_term->add_field( array(
666 'name' => esc_html__( 'Arbitrary Term Field', 'cmb2' ),
667 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
668 'id' => 'yourprefix_term_term_text_field',
669 'type' => 'text',
670 ) );
671
672 }
673
674 add_action( 'cmb2_admin_init', 'yourprefix_register_theme_options_metabox' );
675 /**
676 * Hook in and register a metabox to handle a theme options page and adds a menu item.
677 */
678 function yourprefix_register_theme_options_metabox() {
679
680 /**
681 * Registers options page menu item and form.
682 */
683 $cmb_options = new_cmb2_box( array(
684 'id' => 'yourprefix_theme_options_page',
685 'title' => esc_html__( 'Theme Options', 'cmb2' ),
686 'object_types' => array( 'options-page' ),
687
688 /*
689 * The following parameters are specific to the options-page box
690 * Several of these parameters are passed along to add_menu_page()/add_submenu_page().
691 */
692
693 'option_key' => 'yourprefix_theme_options', // The option key and admin menu page slug.
694 'icon_url' => 'dashicons-palmtree', // Menu icon. Only applicable if 'parent_slug' is left empty.
695 // 'menu_title' => esc_html__( 'Options', 'cmb2' ), // Falls back to 'title' (above).
696 // 'parent_slug' => 'themes.php', // Make options page a submenu item of the themes menu.
697 // 'capability' => 'manage_options', // Cap required to view options-page.
698 // 'position' => 1, // Menu position. Only applicable if 'parent_slug' is left empty.
699 // 'admin_menu_hook' => 'network_admin_menu', // 'network_admin_menu' to add network-level options page.
700 // 'priority' => 10, // Define the page-registration admin menu hook priority.
701 // 'display_cb' => false, // Override the options-page form output (CMB2_Hookup::options_page_output()).
702 // 'save_button' => esc_html__( 'Save Theme Options', 'cmb2' ), // The text for the options-page save button. Defaults to 'Save'.
703 // 'disable_settings_errors' => true, // On settings pages (not options-general.php sub-pages), allows disabling.
704 // 'message_cb' => 'yourprefix_options_page_message_callback',
705 // 'tab_group' => '', // Tab-group identifier, enables options page tab navigation.
706 // 'tab_title' => null, // Falls back to 'title' (above).
707 // 'autoload' => false, // Defaults to true, the options-page option will be autloaded.
708 ) );
709
710 /**
711 * Options fields ids only need
712 * to be unique within this box.
713 * Prefix is not needed.
714 */
715 $cmb_options->add_field( array(
716 'name' => esc_html__( 'Site Background Color', 'cmb2' ),
717 'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
718 'id' => 'bg_color',
719 'type' => 'colorpicker',
720 'default' => '#ffffff',
721 ) );
722
723 }
724
725 /**
726 * Callback to define the optionss-saved message.
727 *
728 * @param CMB2 $cmb The CMB2 object.
729 * @param array $args {
730 * An array of message arguments
731 *
732 * @type bool $is_options_page Whether current page is this options page.
733 * @type bool $should_notify Whether options were saved and we should be notified.
734 * @type bool $is_updated Whether options were updated with save (or stayed the same).
735 * @type string $setting For add_settings_error(), Slug title of the setting to which
736 * this error applies.
737 * @type string $code For add_settings_error(), Slug-name to identify the error.
738 * Used as part of 'id' attribute in HTML output.
739 * @type string $message For add_settings_error(), The formatted message text to display
740 * to the user (will be shown inside styled `<div>` and `<p>` tags).
741 * Will be 'Settings updated.' if $is_updated is true, else 'Nothing to update.'
742 * @type string $type For add_settings_error(), Message type, controls HTML class.
743 * Accepts 'error', 'updated', '', 'notice-warning', etc.
744 * Will be 'updated' if $is_updated is true, else 'notice-warning'.
745 * }
746 */
747 function yourprefix_options_page_message_callback( $cmb, $args ) {
748 if ( ! empty( $args['should_notify'] ) ) {
749
750 if ( $args['is_updated'] ) {
751
752 // Modify the updated message.
753 $args['message'] = sprintf( esc_html__( '%s &mdash; Updated!', 'cmb2' ), $cmb->prop( 'title' ) );
754 }
755
756 add_settings_error( $args['setting'], $args['code'], $args['message'], $args['type'] );
757 }
758 }
759
760 /**
761 * Only show this box in the CMB2 REST API if the user is logged in.
762 *
763 * @param bool $is_allowed Whether this box and its fields are allowed to be viewed.
764 * @param CMB2_REST_Controller $cmb_controller The controller object.
765 * CMB2 object available via `$cmb_controller->rest_box->cmb`.
766 *
767 * @return bool Whether this box and its fields are allowed to be viewed.
768 */
769 function yourprefix_limit_rest_view_to_logged_in_users( $is_allowed, $cmb_controller ) {
770 if ( ! is_user_logged_in() ) {
771 $is_allowed = false;
772 }
773
774 return $is_allowed;
775 }
776
777 add_action( 'cmb2_init', 'yourprefix_register_rest_api_box' );
778 /**
779 * Hook in and add a box to be available in the CMB2 REST API. Can only happen on the 'cmb2_init' hook.
780 * More info: https://github.com/CMB2/CMB2/wiki/REST-API
781 */
782 function yourprefix_register_rest_api_box() {
783 $cmb_rest = new_cmb2_box( array(
784 'id' => 'yourprefix_rest_metabox',
785 'title' => esc_html__( 'REST Test Box', 'cmb2' ),
786 'object_types' => array( 'page' ), // Post type
787 'show_in_rest' => WP_REST_Server::ALLMETHODS, // WP_REST_Server::READABLE|WP_REST_Server::EDITABLE, // Determines which HTTP methods the box is visible in.
788 // Optional callback to limit box visibility.
789 // See: https://github.com/CMB2/CMB2/wiki/REST-API#permissions
790 // 'get_box_permissions_check_cb' => 'yourprefix_limit_rest_view_to_logged_in_users',
791 ) );
792
793 $cmb_rest->add_field( array(
794 'name' => esc_html__( 'REST Test Text', 'cmb2' ),
795 'desc' => esc_html__( 'Will show in the REST API for this box and for pages.', 'cmb2' ),
796 'id' => 'yourprefix_rest_text',
797 'type' => 'text',
798 ) );
799
800 $cmb_rest->add_field( array(
801 'name' => esc_html__( 'REST Editable Test Text', 'cmb2' ),
802 'desc' => esc_html__( 'Will show in REST API "editable" contexts only (`POST` requests).', 'cmb2' ),
803 'id' => 'yourprefix_rest_editable_text',
804 'type' => 'text',
805 '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.
806 ) );
807 }
808