Completed
Push — trunk ( 5d123b...5dfd19 )
by Justin
06:53
created

example-functions.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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/WebDevStudios/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 object $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 ( $cmb->object_id !== get_option( 'page_on_front' ) ) {
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 object $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 $classes; ?>">
69
		<p><label for="<?php echo $id; ?>"><?php echo $label; ?></label></p>
70
		<p><input id="<?php echo $id; ?>" type="text" name="<?php echo $name; ?>" value="<?php echo $value; ?>"/></p>
71
		<p class="description"><?php echo $description; ?></p>
72
	</div>
73
	<?php
74
}
75
76
/**
77
 * Conditionally displays a message if the $post_id is 2
78
 *
79
 * @param  array             $field_args Array of field parameters
80
 * @param  CMB2_Field object $field      Field object
81
 */
82
function yourprefix_before_row_if_2( $field_args, $field ) {
83
	if ( 2 == $field->object_id ) {
84
		echo '<p>Testing <b>"before_row"</b> parameter (on $post_id 2)</p>';
85
	} else {
86
		echo '<p>Testing <b>"before_row"</b> parameter (<b>NOT</b> on $post_id 2)</p>';
87
	}
88
}
89
90
add_action( 'cmb2_admin_init', 'yourprefix_register_demo_metabox' );
91
/**
92
 * Hook in and add a demo metabox. Can only happen on the 'cmb2_admin_init' or 'cmb2_init' hook.
93
 */
94
function yourprefix_register_demo_metabox() {
95
	$prefix = 'yourprefix_demo_';
96
97
	/**
98
	 * Sample metabox to demonstrate each field type included
99
	 */
100
	$cmb_demo = new_cmb2_box( array(
101
		'id'            => $prefix . 'metabox',
102
		'title'         => __( 'Test Metabox', 'cmb2' ),
103
		'object_types'  => array( 'page', ), // Post type
104
		// 'show_on_cb' => 'yourprefix_show_if_front_page', // function should return a bool value
105
		// 'context'    => 'normal',
106
		// 'priority'   => 'high',
107
		// 'show_names' => true, // Show field names on the left
108
		// 'cmb_styles' => false, // false to disable the CMB stylesheet
109
		// 'closed'     => true, // true to keep the metabox closed by default
110
		// 'classes'    => 'extra-class', // Extra cmb2-wrap classes
111
		// 'classes_cb' => 'yourprefix_add_some_classes', // Add classes through a callback.
112
	) );
113
114
	$cmb_demo->add_field( array(
115
		'name'       => __( 'Test Text', 'cmb2' ),
116
		'desc'       => __( 'field description (optional)', 'cmb2' ),
117
		'id'         => $prefix . 'text',
118
		'type'       => 'text',
119
		'show_on_cb' => 'yourprefix_hide_if_no_cats', // function should return a bool value
120
		// 'sanitization_cb' => 'my_custom_sanitization', // custom sanitization callback parameter
121
		// 'escape_cb'       => 'my_custom_escaping',  // custom escaping callback parameter
122
		// 'on_front'        => false, // Optionally designate a field to wp-admin only
123
		// 'repeatable'      => true,
124
	) );
125
126
	$cmb_demo->add_field( array(
127
		'name' => __( 'Test Text Small', 'cmb2' ),
128
		'desc' => __( 'field description (optional)', 'cmb2' ),
129
		'id'   => $prefix . 'textsmall',
130
		'type' => 'text_small',
131
		// 'repeatable' => true,
132
	) );
133
134
	$cmb_demo->add_field( array(
135
		'name' => __( 'Test Text Medium', 'cmb2' ),
136
		'desc' => __( 'field description (optional)', 'cmb2' ),
137
		'id'   => $prefix . 'textmedium',
138
		'type' => 'text_medium',
139
		// 'repeatable' => true,
140
	) );
141
142
	$cmb_demo->add_field( array(
143
		'name' => __( 'Custom Rendered Field', 'cmb2' ),
144
		'desc' => __( 'field description (optional)', 'cmb2' ),
145
		'id'   => $prefix . 'render_row_cb',
146
		'type' => 'text',
147
		'render_row_cb' => 'yourprefix_render_row_cb',
148
	) );
149
150
	$cmb_demo->add_field( array(
151
		'name' => __( 'Website URL', 'cmb2' ),
152
		'desc' => __( 'field description (optional)', 'cmb2' ),
153
		'id'   => $prefix . 'url',
154
		'type' => 'text_url',
155
		// 'protocols' => array('http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet'), // Array of allowed protocols
156
		// 'repeatable' => true,
157
	) );
158
159
	$cmb_demo->add_field( array(
160
		'name' => __( 'Test Text Email', 'cmb2' ),
161
		'desc' => __( 'field description (optional)', 'cmb2' ),
162
		'id'   => $prefix . 'email',
163
		'type' => 'text_email',
164
		// 'repeatable' => true,
165
	) );
166
167
	$cmb_demo->add_field( array(
168
		'name' => __( 'Test Time', 'cmb2' ),
169
		'desc' => __( 'field description (optional)', 'cmb2' ),
170
		'id'   => $prefix . 'time',
171
		'type' => 'text_time',
172
		// 'time_format' => 'H:i', // Set to 24hr format
173
	) );
174
175
	$cmb_demo->add_field( array(
176
		'name' => __( 'Time zone', 'cmb2' ),
177
		'desc' => __( 'Time zone', 'cmb2' ),
178
		'id'   => $prefix . 'timezone',
179
		'type' => 'select_timezone',
180
	) );
181
182
	$cmb_demo->add_field( array(
183
		'name' => __( 'Test Date Picker', 'cmb2' ),
184
		'desc' => __( 'field description (optional)', 'cmb2' ),
185
		'id'   => $prefix . 'textdate',
186
		'type' => 'text_date',
187
		// 'date_format' => 'Y-m-d',
188
	) );
189
190
	$cmb_demo->add_field( array(
191
		'name' => __( 'Test Date Picker (UNIX timestamp)', 'cmb2' ),
192
		'desc' => __( 'field description (optional)', 'cmb2' ),
193
		'id'   => $prefix . 'textdate_timestamp',
194
		'type' => 'text_date_timestamp',
195
		// 'timezone_meta_key' => $prefix . 'timezone', // Optionally make this field honor the timezone selected in the select_timezone specified above
196
	) );
197
198
	$cmb_demo->add_field( array(
199
		'name' => __( 'Test Date/Time Picker Combo (UNIX timestamp)', 'cmb2' ),
200
		'desc' => __( 'field description (optional)', 'cmb2' ),
201
		'id'   => $prefix . 'datetime_timestamp',
202
		'type' => 'text_datetime_timestamp',
203
	) );
204
205
	// This text_datetime_timestamp_timezone field type
206
	// is only compatible with PHP versions 5.3 or above.
207
	// Feel free to uncomment and use if your server meets the requirement
208
	// $cmb_demo->add_field( array(
209
	// 	'name' => __( 'Test Date/Time Picker/Time zone Combo (serialized DateTime object)', 'cmb2' ),
210
	// 	'desc' => __( 'field description (optional)', 'cmb2' ),
211
	// 	'id'   => $prefix . 'datetime_timestamp_timezone',
212
	// 	'type' => 'text_datetime_timestamp_timezone',
213
	// ) );
214
215
	$cmb_demo->add_field( array(
216
		'name' => __( 'Test Money', 'cmb2' ),
217
		'desc' => __( 'field description (optional)', 'cmb2' ),
218
		'id'   => $prefix . 'textmoney',
219
		'type' => 'text_money',
220
		// 'before_field' => '£', // override '$' symbol if needed
221
		// 'repeatable' => true,
222
	) );
223
224
	$cmb_demo->add_field( array(
225
		'name'    => __( 'Test Color Picker', 'cmb2' ),
226
		'desc'    => __( 'field description (optional)', 'cmb2' ),
227
		'id'      => $prefix . 'colorpicker',
228
		'type'    => 'colorpicker',
229
		'default' => '#ffffff',
230
		// 'attributes' => array(
231
		// 	'data-colorpicker' => json_encode( array(
232
		// 		'palettes' => array( '#3dd0cc', '#ff834c', '#4fa2c0', '#0bc991', ),
233
		// 	) ),
234
		// ),
235
	) );
236
237
	$cmb_demo->add_field( array(
238
		'name' => __( 'Test Text Area', 'cmb2' ),
239
		'desc' => __( 'field description (optional)', 'cmb2' ),
240
		'id'   => $prefix . 'textarea',
241
		'type' => 'textarea',
242
	) );
243
244
	$cmb_demo->add_field( array(
245
		'name' => __( 'Test Text Area Small', 'cmb2' ),
246
		'desc' => __( 'field description (optional)', 'cmb2' ),
247
		'id'   => $prefix . 'textareasmall',
248
		'type' => 'textarea_small',
249
	) );
250
251
	$cmb_demo->add_field( array(
252
		'name' => __( 'Test Text Area for Code', 'cmb2' ),
253
		'desc' => __( 'field description (optional)', 'cmb2' ),
254
		'id'   => $prefix . 'textarea_code',
255
		'type' => 'textarea_code',
256
	) );
257
258
	$cmb_demo->add_field( array(
259
		'name' => __( 'Test Title Weeeee', 'cmb2' ),
260
		'desc' => __( 'This is a title description', 'cmb2' ),
261
		'id'   => $prefix . 'title',
262
		'type' => 'title',
263
	) );
264
265
	$cmb_demo->add_field( array(
266
		'name'             => __( 'Test Select', 'cmb2' ),
267
		'desc'             => __( 'field description (optional)', 'cmb2' ),
268
		'id'               => $prefix . 'select',
269
		'type'             => 'select',
270
		'show_option_none' => true,
271
		'options'          => array(
272
			'standard' => __( 'Option One', 'cmb2' ),
273
			'custom'   => __( 'Option Two', 'cmb2' ),
274
			'none'     => __( 'Option Three', 'cmb2' ),
275
		),
276
	) );
277
278
	$cmb_demo->add_field( array(
279
		'name'             => __( 'Test Radio inline', 'cmb2' ),
280
		'desc'             => __( 'field description (optional)', 'cmb2' ),
281
		'id'               => $prefix . 'radio_inline',
282
		'type'             => 'radio_inline',
283
		'show_option_none' => 'No Selection',
284
		'options'          => array(
285
			'standard' => __( 'Option One', 'cmb2' ),
286
			'custom'   => __( 'Option Two', 'cmb2' ),
287
			'none'     => __( 'Option Three', 'cmb2' ),
288
		),
289
	) );
290
291
	$cmb_demo->add_field( array(
292
		'name'    => __( 'Test Radio', 'cmb2' ),
293
		'desc'    => __( 'field description (optional)', 'cmb2' ),
294
		'id'      => $prefix . 'radio',
295
		'type'    => 'radio',
296
		'options' => array(
297
			'option1' => __( 'Option One', 'cmb2' ),
298
			'option2' => __( 'Option Two', 'cmb2' ),
299
			'option3' => __( 'Option Three', 'cmb2' ),
300
		),
301
	) );
302
303
	$cmb_demo->add_field( array(
304
		'name'     => __( 'Test Taxonomy Radio', 'cmb2' ),
305
		'desc'     => __( 'field description (optional)', 'cmb2' ),
306
		'id'       => $prefix . 'text_taxonomy_radio',
307
		'type'     => 'taxonomy_radio',
308
		'taxonomy' => 'category', // Taxonomy Slug
309
		// 'inline'  => true, // Toggles display to inline
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
310
	) );
311
312
	$cmb_demo->add_field( array(
313
		'name'     => __( 'Test Taxonomy Select', 'cmb2' ),
314
		'desc'     => __( 'field description (optional)', 'cmb2' ),
315
		'id'       => $prefix . 'taxonomy_select',
316
		'type'     => 'taxonomy_select',
317
		'taxonomy' => 'category', // Taxonomy Slug
318
	) );
319
320
	$cmb_demo->add_field( array(
321
		'name'     => __( 'Test Taxonomy Multi Checkbox', 'cmb2' ),
322
		'desc'     => __( 'field description (optional)', 'cmb2' ),
323
		'id'       => $prefix . 'multitaxonomy',
324
		'type'     => 'taxonomy_multicheck',
325
		'taxonomy' => 'post_tag', // Taxonomy Slug
326
		// 'inline'  => true, // Toggles display to inline
327
	) );
328
329
	$cmb_demo->add_field( array(
330
		'name' => __( 'Test Checkbox', 'cmb2' ),
331
		'desc' => __( 'field description (optional)', 'cmb2' ),
332
		'id'   => $prefix . 'checkbox',
333
		'type' => 'checkbox',
334
	) );
335
336
	$cmb_demo->add_field( array(
337
		'name'    => __( 'Test Multi Checkbox', 'cmb2' ),
338
		'desc'    => __( 'field description (optional)', 'cmb2' ),
339
		'id'      => $prefix . 'multicheckbox',
340
		'type'    => 'multicheck',
341
		// 'multiple' => true, // Store values in individual rows
342
		'options' => array(
343
			'check1' => __( 'Check One', 'cmb2' ),
344
			'check2' => __( 'Check Two', 'cmb2' ),
345
			'check3' => __( 'Check Three', 'cmb2' ),
346
		),
347
		// 'inline'  => true, // Toggles display to inline
348
	) );
349
350
	$cmb_demo->add_field( array(
351
		'name'    => __( 'Test wysiwyg', 'cmb2' ),
352
		'desc'    => __( 'field description (optional)', 'cmb2' ),
353
		'id'      => $prefix . 'wysiwyg',
354
		'type'    => 'wysiwyg',
355
		'options' => array( 'textarea_rows' => 5, ),
356
	) );
357
358
	$cmb_demo->add_field( array(
359
		'name' => __( 'Test Image', 'cmb2' ),
360
		'desc' => __( 'Upload an image or enter a URL.', 'cmb2' ),
361
		'id'   => $prefix . 'image',
362
		'type' => 'file',
363
	) );
364
365
	$cmb_demo->add_field( array(
366
		'name'         => __( 'Multiple Files', 'cmb2' ),
367
		'desc'         => __( 'Upload or add multiple images/attachments.', 'cmb2' ),
368
		'id'           => $prefix . 'file_list',
369
		'type'         => 'file_list',
370
		'preview_size' => array( 100, 100 ), // Default: array( 50, 50 )
371
	) );
372
373
	$cmb_demo->add_field( array(
374
		'name' => __( 'oEmbed', 'cmb2' ),
375
		'desc' => __( 'Enter a youtube, twitter, or instagram URL. Supports services listed at <a href="http://codex.wordpress.org/Embeds">http://codex.wordpress.org/Embeds</a>.', 'cmb2' ),
376
		'id'   => $prefix . 'embed',
377
		'type' => 'oembed',
378
	) );
379
380
	$cmb_demo->add_field( array(
381
		'name'         => 'Testing Field Parameters',
382
		'id'           => $prefix . 'parameters',
383
		'type'         => 'text',
384
		'before_row'   => 'yourprefix_before_row_if_2', // callback
385
		'before'       => '<p>Testing <b>"before"</b> parameter</p>',
386
		'before_field' => '<p>Testing <b>"before_field"</b> parameter</p>',
387
		'after_field'  => '<p>Testing <b>"after_field"</b> parameter</p>',
388
		'after'        => '<p>Testing <b>"after"</b> parameter</p>',
389
		'after_row'    => '<p>Testing <b>"after_row"</b> parameter</p>',
390
	) );
391
392
}
393
394
add_action( 'cmb2_admin_init', 'yourprefix_register_about_page_metabox' );
395
/**
396
 * Hook in and add a metabox that only appears on the 'About' page
397
 */
398
function yourprefix_register_about_page_metabox() {
399
	$prefix = 'yourprefix_about_';
400
401
	/**
402
	 * Metabox to be displayed on a single page ID
403
	 */
404
	$cmb_about_page = new_cmb2_box( array(
405
		'id'           => $prefix . 'metabox',
406
		'title'        => __( 'About Page Metabox', 'cmb2' ),
407
		'object_types' => array( 'page', ), // Post type
408
		'context'      => 'normal',
409
		'priority'     => 'high',
410
		'show_names'   => true, // Show field names on the left
411
		'show_on'      => array( 'id' => array( 2, ) ), // Specific post IDs to display this metabox
412
	) );
413
414
	$cmb_about_page->add_field( array(
415
		'name' => __( 'Test Text', 'cmb2' ),
416
		'desc' => __( 'field description (optional)', 'cmb2' ),
417
		'id'   => $prefix . 'text',
418
		'type' => 'text',
419
	) );
420
421
}
422
423
add_action( 'cmb2_admin_init', 'yourprefix_register_repeatable_group_field_metabox' );
424
/**
425
 * Hook in and add a metabox to demonstrate repeatable grouped fields
426
 */
427
function yourprefix_register_repeatable_group_field_metabox() {
428
	$prefix = 'yourprefix_group_';
429
430
	/**
431
	 * Repeatable Field Groups
432
	 */
433
	$cmb_group = new_cmb2_box( array(
434
		'id'           => $prefix . 'metabox',
435
		'title'        => __( 'Repeating Field Group', 'cmb2' ),
436
		'object_types' => array( 'page', ),
437
	) );
438
439
	// $group_field_id is the field id string, so in this case: $prefix . 'demo'
440
	$group_field_id = $cmb_group->add_field( array(
441
		'id'          => $prefix . 'demo',
442
		'type'        => 'group',
443
		'description' => __( 'Generates reusable form entries', 'cmb2' ),
444
		'options'     => array(
445
			'group_title'   => __( 'Entry {#}', 'cmb2' ), // {#} gets replaced by row number
446
			'add_button'    => __( 'Add Another Entry', 'cmb2' ),
447
			'remove_button' => __( 'Remove Entry', 'cmb2' ),
448
			'sortable'      => true, // beta
449
			// 'closed'     => true, // true to have the groups closed by default
450
		),
451
	) );
452
453
	/**
454
	 * Group fields works the same, except ids only need
455
	 * to be unique to the group. Prefix is not needed.
456
	 *
457
	 * The parent field's id needs to be passed as the first argument.
458
	 */
459
	$cmb_group->add_group_field( $group_field_id, array(
460
		'name'       => __( 'Entry Title', 'cmb2' ),
461
		'id'         => 'title',
462
		'type'       => 'text',
463
		// 'repeatable' => true, // Repeatable fields are supported w/in repeatable groups (for most types)
464
	) );
465
466
	$cmb_group->add_group_field( $group_field_id, array(
467
		'name'        => __( 'Description', 'cmb2' ),
468
		'description' => __( 'Write a short description for this entry', 'cmb2' ),
469
		'id'          => 'description',
470
		'type'        => 'textarea_small',
471
	) );
472
473
	$cmb_group->add_group_field( $group_field_id, array(
474
		'name' => __( 'Entry Image', 'cmb2' ),
475
		'id'   => 'image',
476
		'type' => 'file',
477
	) );
478
479
	$cmb_group->add_group_field( $group_field_id, array(
480
		'name' => __( 'Image Caption', 'cmb2' ),
481
		'id'   => 'image_caption',
482
		'type' => 'text',
483
	) );
484
485
}
486
487
add_action( 'cmb2_admin_init', 'yourprefix_register_user_profile_metabox' );
488
/**
489
 * Hook in and add a metabox to add fields to the user profile pages
490
 */
491
function yourprefix_register_user_profile_metabox() {
492
	$prefix = 'yourprefix_user_';
493
494
	/**
495
	 * Metabox for the user profile screen
496
	 */
497
	$cmb_user = new_cmb2_box( array(
498
		'id'               => $prefix . 'edit',
499
		'title'            => __( 'User Profile Metabox', 'cmb2' ), // Doesn't output for user boxes
500
		'object_types'     => array( 'user' ), // Tells CMB2 to use user_meta vs post_meta
501
		'show_names'       => true,
502
		'new_user_section' => 'add-new-user', // where form will show on new user page. 'add-existing-user' is only other valid option.
503
	) );
504
505
	$cmb_user->add_field( array(
506
		'name'     => __( 'Extra Info', 'cmb2' ),
507
		'desc'     => __( 'field description (optional)', 'cmb2' ),
508
		'id'       => $prefix . 'extra_info',
509
		'type'     => 'title',
510
		'on_front' => false,
511
	) );
512
513
	$cmb_user->add_field( array(
514
		'name'    => __( 'Avatar', 'cmb2' ),
515
		'desc'    => __( 'field description (optional)', 'cmb2' ),
516
		'id'      => $prefix . 'avatar',
517
		'type'    => 'file',
518
	) );
519
520
	$cmb_user->add_field( array(
521
		'name' => __( 'Facebook URL', 'cmb2' ),
522
		'desc' => __( 'field description (optional)', 'cmb2' ),
523
		'id'   => $prefix . 'facebookurl',
524
		'type' => 'text_url',
525
	) );
526
527
	$cmb_user->add_field( array(
528
		'name' => __( 'Twitter URL', 'cmb2' ),
529
		'desc' => __( 'field description (optional)', 'cmb2' ),
530
		'id'   => $prefix . 'twitterurl',
531
		'type' => 'text_url',
532
	) );
533
534
	$cmb_user->add_field( array(
535
		'name' => __( 'Google+ URL', 'cmb2' ),
536
		'desc' => __( 'field description (optional)', 'cmb2' ),
537
		'id'   => $prefix . 'googleplusurl',
538
		'type' => 'text_url',
539
	) );
540
541
	$cmb_user->add_field( array(
542
		'name' => __( 'Linkedin URL', 'cmb2' ),
543
		'desc' => __( 'field description (optional)', 'cmb2' ),
544
		'id'   => $prefix . 'linkedinurl',
545
		'type' => 'text_url',
546
	) );
547
548
	$cmb_user->add_field( array(
549
		'name' => __( 'User Field', 'cmb2' ),
550
		'desc' => __( 'field description (optional)', 'cmb2' ),
551
		'id'   => $prefix . 'user_text_field',
552
		'type' => 'text',
553
	) );
554
555
}
556
557
add_action( 'cmb2_admin_init', 'yourprefix_register_taxonomy_metabox' );
558
/**
559
 * Hook in and add a metabox to add fields to taxonomy terms
560
 */
561
function yourprefix_register_taxonomy_metabox() {
562
	$prefix = 'yourprefix_term_';
563
564
	/**
565
	 * Metabox to add fields to categories and tags
566
	 */
567
	$cmb_term = new_cmb2_box( array(
568
		'id'               => $prefix . 'edit',
569
		'title'            => __( 'Category Metabox', 'cmb2' ), // Doesn't output for term boxes
570
		'object_types'     => array( 'term' ), // Tells CMB2 to use term_meta vs post_meta
571
		'taxonomies'       => array( 'category', 'post_tag' ), // Tells CMB2 which taxonomies should have these fields
572
		// 'new_term_section' => true, // Will display in the "Add New Category" section
573
	) );
574
575
	$cmb_term->add_field( array(
576
		'name'     => __( 'Extra Info', 'cmb2' ),
577
		'desc'     => __( 'field description (optional)', 'cmb2' ),
578
		'id'       => $prefix . 'extra_info',
579
		'type'     => 'title',
580
		'on_front' => false,
581
	) );
582
583
	$cmb_term->add_field( array(
584
		'name' => __( 'Term Image', 'cmb2' ),
585
		'desc' => __( 'field description (optional)', 'cmb2' ),
586
		'id'   => $prefix . 'avatar',
587
		'type' => 'file',
588
	) );
589
590
	$cmb_term->add_field( array(
591
		'name' => __( 'Arbitrary Term Field', 'cmb2' ),
592
		'desc' => __( 'field description (optional)', 'cmb2' ),
593
		'id'   => $prefix . 'term_text_field',
594
		'type' => 'text',
595
	) );
596
597
}
598
599
add_action( 'cmb2_admin_init', 'yourprefix_register_theme_options_metabox' );
600
/**
601
 * Hook in and register a metabox to handle a theme options page
602
 */
603
function yourprefix_register_theme_options_metabox() {
604
605
	$option_key = 'yourprefix_theme_options';
606
607
	/**
608
	 * Metabox for an options page. Will not be added automatically, but needs to be called with
609
	 * the `cmb2_metabox_form` helper function. See wiki for more info.
610
	 */
611
	$cmb_options = new_cmb2_box( array(
612
		'id'      => $option_key . 'page',
613
		'title'   => __( 'Theme Options Metabox', 'cmb2' ),
614
		'hookup'  => false, // Do not need the normal user/post hookup
615
		'show_on' => array(
616
			// These are important, don't remove
617
			'key'   => 'options-page',
618
			'value' => array( $option_key )
619
		),
620
	) );
621
622
	/**
623
	 * Options fields ids only need
624
	 * to be unique within this option group.
625
	 * Prefix is not needed.
626
	 */
627
	$cmb_options->add_field( array(
628
		'name'    => __( 'Site Background Color', 'cmb2' ),
629
		'desc'    => __( 'field description (optional)', 'cmb2' ),
630
		'id'      => 'bg_color',
631
		'type'    => 'colorpicker',
632
		'default' => '#ffffff',
633
	) );
634
635
}
636