Completed
Pull Request — trunk (#541)
by Justin
05:57
created

example-functions.php (43 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
 * Conditionally displays a message if the $post_id is 2
78
 *
79
 * @param  array             $field_args Array of field parameters
80
 * @param  CMB2_Field object $field      Field object
81
 */
82
function yourprefix_before_row_if_2( $field_args, $field ) {
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
	if ( 2 == $field->object_id ) {
84
		echo '<p>Testing <b>"before_row"</b> parameter (on $post_id 2)</p>';
85
	} else {
86
		echo '<p>Testing <b>"before_row"</b> parameter (<b>NOT</b> on $post_id 2)</p>';
87
	}
88
}
89
90
add_action( 'cmb2_admin_init', 'yourprefix_register_demo_metabox' );
91
/**
92
 * Hook in and add a demo metabox. Can only happen on the 'cmb2_admin_init' or 'cmb2_init' hook.
93
 */
94
function yourprefix_register_demo_metabox() {
95
	$prefix = 'yourprefix_demo_';
96
97
	/**
98
	 * Sample metabox to demonstrate each field type included
99
	 */
100
	$cmb_demo = new_cmb2_box( array(
101
		'id'              => $prefix . 'metabox',
102
		'title'           => __( 'Test Metabox', 'cmb2' ),
103
		'object_types'    => array( 'page', ), // Post type
104
		// 'show_on_cb'   => 'yourprefix_show_if_front_page', // function should return a bool value
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...
105
		// '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...
106
		// '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...
107
		// '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...
108
		// '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...
109
		// '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...
110
		// 'show_in_rest' => true, // true to show fields in the WP-API. More here: <docs link>
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
111
		// '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...
112
		// '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...
113
	) );
114
115
	$cmb_demo->add_field( array(
116
		'name'       => __( 'Test Text', 'cmb2' ),
117
		'desc'       => __( 'field description (optional)', 'cmb2' ),
118
		'id'         => $prefix . 'text',
119
		'type'       => 'text',
120
		'show_on_cb' => 'yourprefix_hide_if_no_cats', // function should return a bool value
121
		// '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...
122
		// '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...
123
		// '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...
124
		// '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...
125
		// 'show_in_rest'    => false, // false to remove field from WP-API, if cmb 'show_in_rest' is true. More here: <docs link>
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

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