Completed
Push — trunk ( 5dfd19...835891 )
by Justin
07:26
created

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