Completed
Pull Request — trunk (#541)
by Justin
07:09
created

example-functions.php (48 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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 31 and the first side effect is on line 19.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

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