Completed
Push — trunk ( 38cf29...27162a )
by Justin
08:07
created

example-functions.php (4 issues)

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
 * Conditionally displays a message if the $post_id is 2
56
 *
57
 * @param  array             $field_args Array of field parameters
58
 * @param  CMB2_Field object $field      Field object
59
 */
60
function yourprefix_before_row_if_2( $field_args, $field ) {
61
	if ( 2 == $field->object_id ) {
62
		echo '<p>Testing <b>"before_row"</b> parameter (on $post_id 2)</p>';
63
	} else {
64
		echo '<p>Testing <b>"before_row"</b> parameter (<b>NOT</b> on $post_id 2)</p>';
65
	}
66
}
67
68
add_action( 'cmb2_admin_init', 'yourprefix_register_demo_metabox' );
69
/**
70
 * Hook in and add a demo metabox. Can only happen on the 'cmb2_admin_init' or 'cmb2_init' hook.
71
 */
72
function yourprefix_register_demo_metabox() {
73
	$prefix = 'yourprefix_demo_';
74
75
	/**
76
	 * Sample metabox to demonstrate each field type included
77
	 */
78
	$cmb_demo = new_cmb2_box( array(
79
		'id'            => $prefix . 'metabox',
80
		'title'         => __( 'Test Metabox', 'cmb2' ),
81
		'object_types'  => array( 'page', ), // Post type
82
		// 'show_on_cb' => 'yourprefix_show_if_front_page', // function should return a bool value
83
		// 'context'    => 'normal',
84
		// 'priority'   => 'high',
85
		// 'show_names' => true, // Show field names on the left
86
		// 'cmb_styles' => false, // false to disable the CMB stylesheet
87
		// 'closed'     => true, // true to keep the metabox closed by default
88
	) );
89
90
	$cmb_demo->add_field( array(
91
		'name'       => __( 'Test Text', 'cmb2' ),
92
		'desc'       => __( 'field description (optional)', 'cmb2' ),
93
		'id'         => $prefix . 'text',
94
		'type'       => 'text',
95
		'show_on_cb' => 'yourprefix_hide_if_no_cats', // function should return a bool value
96
		// 'sanitization_cb' => 'my_custom_sanitization', // custom sanitization callback parameter
97
		// 'escape_cb'       => 'my_custom_escaping',  // custom escaping callback parameter
98
		// 'on_front'        => false, // Optionally designate a field to wp-admin only
99
		// 'repeatable'      => true,
100
	) );
101
102
	$cmb_demo->add_field( array(
103
		'name' => __( 'Test Text Small', 'cmb2' ),
104
		'desc' => __( 'field description (optional)', 'cmb2' ),
105
		'id'   => $prefix . 'textsmall',
106
		'type' => 'text_small',
107
		// 'repeatable' => true,
108
	) );
109
110
	$cmb_demo->add_field( array(
111
		'name' => __( 'Test Text Medium', 'cmb2' ),
112
		'desc' => __( 'field description (optional)', 'cmb2' ),
113
		'id'   => $prefix . 'textmedium',
114
		'type' => 'text_medium',
115
		// 'repeatable' => true,
116
	) );
117
118
	$cmb_demo->add_field( array(
119
		'name' => __( 'Website URL', 'cmb2' ),
120
		'desc' => __( 'field description (optional)', 'cmb2' ),
121
		'id'   => $prefix . 'url',
122
		'type' => 'text_url',
123
		// 'protocols' => array('http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet'), // Array of allowed protocols
124
		// 'repeatable' => true,
125
	) );
126
127
	$cmb_demo->add_field( array(
128
		'name' => __( 'Test Text Email', 'cmb2' ),
129
		'desc' => __( 'field description (optional)', 'cmb2' ),
130
		'id'   => $prefix . 'email',
131
		'type' => 'text_email',
132
		// 'repeatable' => true,
133
	) );
134
135
	$cmb_demo->add_field( array(
136
		'name' => __( 'Test Time', 'cmb2' ),
137
		'desc' => __( 'field description (optional)', 'cmb2' ),
138
		'id'   => $prefix . 'time',
139
		'type' => 'text_time',
140
		// 'attributes' => array(
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
141
		// 	'data-timepicker' => json_encode( array(
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
142
		// 		'timeFormat' => 'HH:mm',
143
		// 	) ),
144
		// ),
145
	) );
146
147
	$cmb_demo->add_field( array(
148
		'name' => __( 'Time zone', 'cmb2' ),
149
		'desc' => __( 'Time zone', 'cmb2' ),
150
		'id'   => $prefix . 'timezone',
151
		'type' => 'select_timezone',
152
	) );
153
154
	$cmb_demo->add_field( array(
155
		'name' => __( 'Test Date Picker', 'cmb2' ),
156
		'desc' => __( 'field description (optional)', 'cmb2' ),
157
		'id'   => $prefix . 'textdate',
158
		'type' => 'text_date',
159
		// 'attributes' => array(
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
160
		// 	// CMB2 checks for datepicker override data here:
161
		// 	'data-datepicker' => json_encode( array(
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
162
		// 		'dateFormat' => 'yy-mm-dd', // override date format
163
		// 	) ),
164
		// ),
165
	) );
166
167
	$cmb_demo->add_field( array(
168
		'name' => __( 'Test Date Picker (UNIX timestamp)', 'cmb2' ),
169
		'desc' => __( 'field description (optional)', 'cmb2' ),
170
		'id'   => $prefix . 'textdate_timestamp',
171
		'type' => 'text_date_timestamp',
172
		// 'timezone_meta_key' => $prefix . 'timezone', // Optionally make this field honor the timezone selected in the select_timezone specified above
173
	) );
174
175
	$cmb_demo->add_field( array(
176
		'name' => __( 'Test Date/Time Picker Combo (UNIX timestamp)', 'cmb2' ),
177
		'desc' => __( 'field description (optional)', 'cmb2' ),
178
		'id'   => $prefix . 'datetime_timestamp',
179
		'type' => 'text_datetime_timestamp',
180
	) );
181
182
	// This text_datetime_timestamp_timezone field type
183
	// is only compatible with PHP versions 5.3 or above.
184
	// Feel free to uncomment and use if your server meets the requirement
185
	// $cmb_demo->add_field( array(
186
	// 	'name' => __( 'Test Date/Time Picker/Time zone Combo (serialized DateTime object)', 'cmb2' ),
187
	// 	'desc' => __( 'field description (optional)', 'cmb2' ),
188
	// 	'id'   => $prefix . 'datetime_timestamp_timezone',
189
	// 	'type' => 'text_datetime_timestamp_timezone',
190
	// ) );
191
192
	$cmb_demo->add_field( array(
193
		'name' => __( 'Test Money', 'cmb2' ),
194
		'desc' => __( 'field description (optional)', 'cmb2' ),
195
		'id'   => $prefix . 'textmoney',
196
		'type' => 'text_money',
197
		// 'before_field' => '£', // override '$' symbol if needed
198
		// 'repeatable' => true,
199
	) );
200
201
	$cmb_demo->add_field( array(
202
		'name'    => __( 'Test Color Picker', 'cmb2' ),
203
		'desc'    => __( 'field description (optional)', 'cmb2' ),
204
		'id'      => $prefix . 'colorpicker',
205
		'type'    => 'colorpicker',
206
		'default' => '#ffffff',
207
		// 'attributes' => array(
208
		// 	'data-colorpicker' => json_encode( array(
209
		// 		'palettes' => array( '#3dd0cc', '#ff834c', '#4fa2c0', '#0bc991', ),
210
		// 	) ),
211
		// ),
212
	) );
213
214
	$cmb_demo->add_field( array(
215
		'name' => __( 'Test Text Area', 'cmb2' ),
216
		'desc' => __( 'field description (optional)', 'cmb2' ),
217
		'id'   => $prefix . 'textarea',
218
		'type' => 'textarea',
219
	) );
220
221
	$cmb_demo->add_field( array(
222
		'name' => __( 'Test Text Area Small', 'cmb2' ),
223
		'desc' => __( 'field description (optional)', 'cmb2' ),
224
		'id'   => $prefix . 'textareasmall',
225
		'type' => 'textarea_small',
226
	) );
227
228
	$cmb_demo->add_field( array(
229
		'name' => __( 'Test Text Area for Code', 'cmb2' ),
230
		'desc' => __( 'field description (optional)', 'cmb2' ),
231
		'id'   => $prefix . 'textarea_code',
232
		'type' => 'textarea_code',
233
	) );
234
235
	$cmb_demo->add_field( array(
236
		'name' => __( 'Test Title Weeeee', 'cmb2' ),
237
		'desc' => __( 'This is a title description', 'cmb2' ),
238
		'id'   => $prefix . 'title',
239
		'type' => 'title',
240
	) );
241
242
	$cmb_demo->add_field( array(
243
		'name'             => __( 'Test Select', 'cmb2' ),
244
		'desc'             => __( 'field description (optional)', 'cmb2' ),
245
		'id'               => $prefix . 'select',
246
		'type'             => 'select',
247
		'show_option_none' => true,
248
		'options'          => array(
249
			'standard' => __( 'Option One', 'cmb2' ),
250
			'custom'   => __( 'Option Two', 'cmb2' ),
251
			'none'     => __( 'Option Three', 'cmb2' ),
252
		),
253
	) );
254
255
	$cmb_demo->add_field( array(
256
		'name'             => __( 'Test Radio inline', 'cmb2' ),
257
		'desc'             => __( 'field description (optional)', 'cmb2' ),
258
		'id'               => $prefix . 'radio_inline',
259
		'type'             => 'radio_inline',
260
		'show_option_none' => 'No Selection',
261
		'options'          => array(
262
			'standard' => __( 'Option One', 'cmb2' ),
263
			'custom'   => __( 'Option Two', 'cmb2' ),
264
			'none'     => __( 'Option Three', 'cmb2' ),
265
		),
266
	) );
267
268
	$cmb_demo->add_field( array(
269
		'name'    => __( 'Test Radio', 'cmb2' ),
270
		'desc'    => __( 'field description (optional)', 'cmb2' ),
271
		'id'      => $prefix . 'radio',
272
		'type'    => 'radio',
273
		'options' => array(
274
			'option1' => __( 'Option One', 'cmb2' ),
275
			'option2' => __( 'Option Two', 'cmb2' ),
276
			'option3' => __( 'Option Three', 'cmb2' ),
277
		),
278
	) );
279
280
	$cmb_demo->add_field( array(
281
		'name'     => __( 'Test Taxonomy Radio', 'cmb2' ),
282
		'desc'     => __( 'field description (optional)', 'cmb2' ),
283
		'id'       => $prefix . 'text_taxonomy_radio',
284
		'type'     => 'taxonomy_radio',
285
		'taxonomy' => 'category', // Taxonomy Slug
286
		// 'inline'  => true, // Toggles display to inline
287
	) );
288
289
	$cmb_demo->add_field( array(
290
		'name'     => __( 'Test Taxonomy Select', 'cmb2' ),
291
		'desc'     => __( 'field description (optional)', 'cmb2' ),
292
		'id'       => $prefix . 'taxonomy_select',
293
		'type'     => 'taxonomy_select',
294
		'taxonomy' => 'category', // Taxonomy Slug
295
	) );
296
297
	$cmb_demo->add_field( array(
298
		'name'     => __( 'Test Taxonomy Multi Checkbox', 'cmb2' ),
299
		'desc'     => __( 'field description (optional)', 'cmb2' ),
300
		'id'       => $prefix . 'multitaxonomy',
301
		'type'     => 'taxonomy_multicheck',
302
		'taxonomy' => 'post_tag', // Taxonomy Slug
303
		// 'inline'  => true, // Toggles display to inline
304
	) );
305
306
	$cmb_demo->add_field( array(
307
		'name' => __( 'Test Checkbox', 'cmb2' ),
308
		'desc' => __( 'field description (optional)', 'cmb2' ),
309
		'id'   => $prefix . 'checkbox',
310
		'type' => 'checkbox',
311
	) );
312
313
	$cmb_demo->add_field( array(
314
		'name'    => __( 'Test Multi Checkbox', 'cmb2' ),
315
		'desc'    => __( 'field description (optional)', 'cmb2' ),
316
		'id'      => $prefix . 'multicheckbox',
317
		'type'    => 'multicheck',
318
		// 'multiple' => true, // Store values in individual rows
319
		'options' => array(
320
			'check1' => __( 'Check One', 'cmb2' ),
321
			'check2' => __( 'Check Two', 'cmb2' ),
322
			'check3' => __( 'Check Three', 'cmb2' ),
323
		),
324
		// 'inline'  => true, // Toggles display to inline
325
	) );
326
327
	$cmb_demo->add_field( array(
328
		'name'    => __( 'Test wysiwyg', 'cmb2' ),
329
		'desc'    => __( 'field description (optional)', 'cmb2' ),
330
		'id'      => $prefix . 'wysiwyg',
331
		'type'    => 'wysiwyg',
332
		'options' => array( 'textarea_rows' => 5, ),
333
	) );
334
335
	$cmb_demo->add_field( array(
336
		'name' => __( 'Test Image', 'cmb2' ),
337
		'desc' => __( 'Upload an image or enter a URL.', 'cmb2' ),
338
		'id'   => $prefix . 'image',
339
		'type' => 'file',
340
	) );
341
342
	$cmb_demo->add_field( array(
343
		'name'         => __( 'Multiple Files', 'cmb2' ),
344
		'desc'         => __( 'Upload or add multiple images/attachments.', 'cmb2' ),
345
		'id'           => $prefix . 'file_list',
346
		'type'         => 'file_list',
347
		'preview_size' => array( 100, 100 ), // Default: array( 50, 50 )
348
	) );
349
350
	$cmb_demo->add_field( array(
351
		'name' => __( 'oEmbed', 'cmb2' ),
352
		'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' ),
353
		'id'   => $prefix . 'embed',
354
		'type' => 'oembed',
355
	) );
356
357
	$cmb_demo->add_field( array(
358
		'name'         => 'Testing Field Parameters',
359
		'id'           => $prefix . 'parameters',
360
		'type'         => 'text',
361
		'before_row'   => 'yourprefix_before_row_if_2', // callback
362
		'before'       => '<p>Testing <b>"before"</b> parameter</p>',
363
		'before_field' => '<p>Testing <b>"before_field"</b> parameter</p>',
364
		'after_field'  => '<p>Testing <b>"after_field"</b> parameter</p>',
365
		'after'        => '<p>Testing <b>"after"</b> parameter</p>',
366
		'after_row'    => '<p>Testing <b>"after_row"</b> parameter</p>',
367
	) );
368
369
}
370
371
add_action( 'cmb2_admin_init', 'yourprefix_register_about_page_metabox' );
372
/**
373
 * Hook in and add a metabox that only appears on the 'About' page
374
 */
375
function yourprefix_register_about_page_metabox() {
376
	$prefix = 'yourprefix_about_';
377
378
	/**
379
	 * Metabox to be displayed on a single page ID
380
	 */
381
	$cmb_about_page = new_cmb2_box( array(
382
		'id'           => $prefix . 'metabox',
383
		'title'        => __( 'About Page Metabox', 'cmb2' ),
384
		'object_types' => array( 'page', ), // Post type
385
		'context'      => 'normal',
386
		'priority'     => 'high',
387
		'show_names'   => true, // Show field names on the left
388
		'show_on'      => array( 'id' => array( 2, ) ), // Specific post IDs to display this metabox
389
	) );
390
391
	$cmb_about_page->add_field( array(
392
		'name' => __( 'Test Text', 'cmb2' ),
393
		'desc' => __( 'field description (optional)', 'cmb2' ),
394
		'id'   => $prefix . 'text',
395
		'type' => 'text',
396
	) );
397
398
}
399
400
add_action( 'cmb2_admin_init', 'yourprefix_register_repeatable_group_field_metabox' );
401
/**
402
 * Hook in and add a metabox to demonstrate repeatable grouped fields
403
 */
404
function yourprefix_register_repeatable_group_field_metabox() {
405
	$prefix = 'yourprefix_group_';
406
407
	/**
408
	 * Repeatable Field Groups
409
	 */
410
	$cmb_group = new_cmb2_box( array(
411
		'id'           => $prefix . 'metabox',
412
		'title'        => __( 'Repeating Field Group', 'cmb2' ),
413
		'object_types' => array( 'page', ),
414
	) );
415
416
	// $group_field_id is the field id string, so in this case: $prefix . 'demo'
417
	$group_field_id = $cmb_group->add_field( array(
418
		'id'          => $prefix . 'demo',
419
		'type'        => 'group',
420
		'description' => __( 'Generates reusable form entries', 'cmb2' ),
421
		'options'     => array(
422
			'group_title'   => __( 'Entry {#}', 'cmb2' ), // {#} gets replaced by row number
423
			'add_button'    => __( 'Add Another Entry', 'cmb2' ),
424
			'remove_button' => __( 'Remove Entry', 'cmb2' ),
425
			'sortable'      => true, // beta
426
			// 'closed'     => true, // true to have the groups closed by default
427
		),
428
	) );
429
430
	/**
431
	 * Group fields works the same, except ids only need
432
	 * to be unique to the group. Prefix is not needed.
433
	 *
434
	 * The parent field's id needs to be passed as the first argument.
435
	 */
436
	$cmb_group->add_group_field( $group_field_id, array(
437
		'name'       => __( 'Entry Title', 'cmb2' ),
438
		'id'         => 'title',
439
		'type'       => 'text',
440
		// 'repeatable' => true, // Repeatable fields are supported w/in repeatable groups (for most types)
441
	) );
442
443
	$cmb_group->add_group_field( $group_field_id, array(
444
		'name'        => __( 'Description', 'cmb2' ),
445
		'description' => __( 'Write a short description for this entry', 'cmb2' ),
446
		'id'          => 'description',
447
		'type'        => 'textarea_small',
448
	) );
449
450
	$cmb_group->add_group_field( $group_field_id, array(
451
		'name' => __( 'Entry Image', 'cmb2' ),
452
		'id'   => 'image',
453
		'type' => 'file',
454
	) );
455
456
	$cmb_group->add_group_field( $group_field_id, array(
457
		'name' => __( 'Image Caption', 'cmb2' ),
458
		'id'   => 'image_caption',
459
		'type' => 'text',
460
	) );
461
462
}
463
464
add_action( 'cmb2_admin_init', 'yourprefix_register_user_profile_metabox' );
465
/**
466
 * Hook in and add a metabox to add fields to the user profile pages
467
 */
468
function yourprefix_register_user_profile_metabox() {
469
	$prefix = 'yourprefix_user_';
470
471
	/**
472
	 * Metabox for the user profile screen
473
	 */
474
	$cmb_user = new_cmb2_box( array(
475
		'id'               => $prefix . 'edit',
476
		'title'            => __( 'User Profile Metabox', 'cmb2' ),
477
		'object_types'     => array( 'user' ), // Tells CMB2 to use user_meta vs post_meta
478
		'show_names'       => true,
479
		'new_user_section' => 'add-new-user', // where form will show on new user page. 'add-existing-user' is only other valid option.
480
	) );
481
482
	$cmb_user->add_field( array(
483
		'name'     => __( 'Extra Info', 'cmb2' ),
484
		'desc'     => __( 'field description (optional)', 'cmb2' ),
485
		'id'       => $prefix . 'extra_info',
486
		'type'     => 'title',
487
		'on_front' => false,
488
	) );
489
490
	$cmb_user->add_field( array(
491
		'name'    => __( 'Avatar', 'cmb2' ),
492
		'desc'    => __( 'field description (optional)', 'cmb2' ),
493
		'id'      => $prefix . 'avatar',
494
		'type'    => 'file',
495
	) );
496
497
	$cmb_user->add_field( array(
498
		'name' => __( 'Facebook URL', 'cmb2' ),
499
		'desc' => __( 'field description (optional)', 'cmb2' ),
500
		'id'   => $prefix . 'facebookurl',
501
		'type' => 'text_url',
502
	) );
503
504
	$cmb_user->add_field( array(
505
		'name' => __( 'Twitter URL', 'cmb2' ),
506
		'desc' => __( 'field description (optional)', 'cmb2' ),
507
		'id'   => $prefix . 'twitterurl',
508
		'type' => 'text_url',
509
	) );
510
511
	$cmb_user->add_field( array(
512
		'name' => __( 'Google+ URL', 'cmb2' ),
513
		'desc' => __( 'field description (optional)', 'cmb2' ),
514
		'id'   => $prefix . 'googleplusurl',
515
		'type' => 'text_url',
516
	) );
517
518
	$cmb_user->add_field( array(
519
		'name' => __( 'Linkedin URL', 'cmb2' ),
520
		'desc' => __( 'field description (optional)', 'cmb2' ),
521
		'id'   => $prefix . 'linkedinurl',
522
		'type' => 'text_url',
523
	) );
524
525
	$cmb_user->add_field( array(
526
		'name' => __( 'User Field', 'cmb2' ),
527
		'desc' => __( 'field description (optional)', 'cmb2' ),
528
		'id'   => $prefix . 'user_text_field',
529
		'type' => 'text',
530
	) );
531
532
}
533
534
add_action( 'cmb2_admin_init', 'yourprefix_register_taxonomy_metabox' );
535
/**
536
 * Hook in and add a metabox to add fields to taxonomy terms
537
 */
538
function yourprefix_register_taxonomy_metabox() {
539
	$prefix = 'yourprefix_term_';
540
541
	/**
542
	 * Metabox to add fields to categories and tags
543
	 */
544
	$cmb_term = new_cmb2_box( array(
545
		'id'               => $prefix . 'edit',
546
		'title'            => __( 'User Profile Metabox', 'cmb2' ),
547
		'object_types'     => array( 'term' ), // Tells CMB2 to use term_meta vs post_meta
548
		'taxonomies'       => array( 'category', 'post_tag' ), // Tells CMB2 which taxonomies should have these fields
549
		// 'new_term_section' => true, // Will display in the "Add New Category" section
550
	) );
551
552
	$cmb_term->add_field( array(
553
		'name'     => __( 'Extra Info', 'cmb2' ),
554
		'desc'     => __( 'field description (optional)', 'cmb2' ),
555
		'id'       => $prefix . 'extra_info',
556
		'type'     => 'title',
557
		'on_front' => false,
558
	) );
559
560
	$cmb_term->add_field( array(
561
		'name' => __( 'Term Image', 'cmb2' ),
562
		'desc' => __( 'field description (optional)', 'cmb2' ),
563
		'id'   => $prefix . 'avatar',
564
		'type' => 'file',
565
	) );
566
567
	$cmb_term->add_field( array(
568
		'name' => __( 'Arbitrary Term Field', 'cmb2' ),
569
		'desc' => __( 'field description (optional)', 'cmb2' ),
570
		'id'   => $prefix . 'term_text_field',
571
		'type' => 'text',
572
	) );
573
574
}
575
576
add_action( 'cmb2_admin_init', 'yourprefix_register_theme_options_metabox' );
577
/**
578
 * Hook in and register a metabox to handle a theme options page
579
 */
580
function yourprefix_register_theme_options_metabox() {
581
582
	$option_key = 'yourprefix_theme_options';
583
584
	/**
585
	 * Metabox for an options page. Will not be added automatically, but needs to be called with
586
	 * the `cmb2_metabox_form` helper function. See wiki for more info.
587
	 */
588
	$cmb_options = new_cmb2_box( array(
589
		'id'      => $option_key . 'page',
590
		'title'   => __( 'Theme Options Metabox', 'cmb2' ),
591
		'hookup'  => false, // Do not need the normal user/post hookup
592
		'show_on' => array(
593
			// These are important, don't remove
594
			'key'   => 'options-page',
595
			'value' => array( $option_key )
596
		),
597
	) );
598
599
	/**
600
	 * Options fields ids only need
601
	 * to be unique within this option group.
602
	 * Prefix is not needed.
603
	 */
604
	$cmb_options->add_field( array(
605
		'name'    => __( 'Site Background Color', 'cmb2' ),
606
		'desc'    => __( 'field description (optional)', 'cmb2' ),
607
		'id'      => 'bg_color',
608
		'type'    => 'colorpicker',
609
		'default' => '#ffffff',
610
	) );
611
612
}
613