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

example-functions.php (6 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 ) {
0 ignored issues
show
The parameter $field_args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

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