Completed
Push — trunk ( 28b86f...295b81 )
by Justin
05:57
created

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