Completed
Pull Request — trunk (#541)
by Justin
40:31 queued 37:55
created

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