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