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