PluginProbe
WPGet API – Connect to any external REST API / trunk
WPGet API – Connect to any external REST API vtrunk
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 trunk, at lib/cmb2/example-functions.php

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