Completed
Pull Request — trunk (#584)
by Juliette
05:40
created

example-functions.php (37 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
 * Conditionally displays a message if the $post_id is 2
56
 *
57
 * @param  array             $field_args Array of field parameters
58
 * @param  CMB2_Field object $field      Field object
59
 */
60
function yourprefix_before_row_if_2( $field_args, $field ) {
61
	if ( 2 == $field->object_id ) {
62
		echo '<p>Testing <b>"before_row"</b> parameter (on $post_id 2)</p>';
63
	} else {
64
		echo '<p>Testing <b>"before_row"</b> parameter (<b>NOT</b> on $post_id 2)</p>';
65
	}
66
}
67
68
add_action( 'cmb2_admin_init', 'yourprefix_register_demo_metabox' );
69
/**
70
 * Hook in and add a demo metabox. Can only happen on the 'cmb2_admin_init' or 'cmb2_init' hook.
71
 */
72
function yourprefix_register_demo_metabox() {
73
	$prefix = 'yourprefix_demo_';
74
75
	/**
76
	 * Sample metabox to demonstrate each field type included
77
	 */
78
	$cmb_demo = new_cmb2_box( array(
79
		'id'            => $prefix . 'metabox',
80
		'title'         => __( 'Test Metabox', 'cmb2' ),
81
		'object_types'  => array( 'page', ), // Post type
82
		// 'show_on_cb' => 'yourprefix_show_if_front_page', // function should return a bool value
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...
83
		// '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...
84
		// '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...
85
		// 'show_names' => true, // Show field names on the left
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...
86
		// 'cmb_styles' => false, // false to disable the CMB stylesheet
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...
87
		// 'closed'     => true, // true to keep the metabox closed by default
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...
88
	) );
89
90
	$cmb_demo->add_field( array(
91
		'name'       => __( 'Test Text', 'cmb2' ),
92
		'desc'       => __( 'field description (optional)', 'cmb2' ),
93
		'id'         => $prefix . 'text',
94
		'type'       => 'text',
95
		'show_on_cb' => 'yourprefix_hide_if_no_cats', // function should return a bool value
96
		// 'sanitization_cb' => 'my_custom_sanitization', // custom sanitization callback parameter
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...
97
		// 'escape_cb'       => 'my_custom_escaping',  // custom escaping callback parameter
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...
98
		// 'on_front'        => false, // Optionally designate a field to wp-admin only
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...
99
		// '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...
100
	) );
101
102
	$cmb_demo->add_field( array(
103
		'name' => __( 'Test Text Small', 'cmb2' ),
104
		'desc' => __( 'field description (optional)', 'cmb2' ),
105
		'id'   => $prefix . 'textsmall',
106
		'type' => 'text_small',
107
		// '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...
108
	) );
109
110
	$cmb_demo->add_field( array(
111
		'name' => __( 'Test Text Medium', 'cmb2' ),
112
		'desc' => __( 'field description (optional)', 'cmb2' ),
113
		'id'   => $prefix . 'textmedium',
114
		'type' => 'text_medium',
115
		// '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...
116
	) );
117
118
	$cmb_demo->add_field( array(
119
		'name' => __( 'Website URL', 'cmb2' ),
120
		'desc' => __( 'field description (optional)', 'cmb2' ),
121
		'id'   => $prefix . 'url',
122
		'type' => 'text_url',
123
		// '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
66% 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
	) );
126
127
	$cmb_demo->add_field( array(
128
		'name' => __( 'Test Text Email', 'cmb2' ),
129
		'desc' => __( 'field description (optional)', 'cmb2' ),
130
		'id'   => $prefix . 'email',
131
		'type' => 'text_email',
132
		// '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...
133
	) );
134
135
	$cmb_demo->add_field( array(
136
		'name' => __( 'Test Time', 'cmb2' ),
137
		'desc' => __( 'field description (optional)', 'cmb2' ),
138
		'id'   => $prefix . 'time',
139
		'type' => 'text_time',
140
		// 'time_format' => 'H:i', // Set to 24hr format
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...
141
	) );
142
143
	$cmb_demo->add_field( array(
144
		'name' => __( 'Time zone', 'cmb2' ),
145
		'desc' => __( 'Time zone', 'cmb2' ),
146
		'id'   => $prefix . 'timezone',
147
		'type' => 'select_timezone',
148
	) );
149
150
	$cmb_demo->add_field( array(
151
		'name' => __( 'Test Date Picker', 'cmb2' ),
152
		'desc' => __( 'field description (optional)', 'cmb2' ),
153
		'id'   => $prefix . 'textdate',
154
		'type' => 'text_date',
155
		// '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...
156
	) );
157
158
	$cmb_demo->add_field( array(
159
		'name' => __( 'Test Date Picker (UNIX timestamp)', 'cmb2' ),
160
		'desc' => __( 'field description (optional)', 'cmb2' ),
161
		'id'   => $prefix . 'textdate_timestamp',
162
		'type' => 'text_date_timestamp',
163
		// '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
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...
164
	) );
165
166
	$cmb_demo->add_field( array(
167
		'name' => __( 'Test Date/Time Picker Combo (UNIX timestamp)', 'cmb2' ),
168
		'desc' => __( 'field description (optional)', 'cmb2' ),
169
		'id'   => $prefix . 'datetime_timestamp',
170
		'type' => 'text_datetime_timestamp',
171
	) );
172
173
	// This text_datetime_timestamp_timezone field type
174
	// is only compatible with PHP versions 5.3 or above.
175
	// Feel free to uncomment and use if your server meets the requirement
176
	// $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...
177
	// 	'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...
178
	// 	'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...
179
	// 	'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...
180
	// 	'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...
181
	// ) );
182
183
	$cmb_demo->add_field( array(
184
		'name' => __( 'Test Money', 'cmb2' ),
185
		'desc' => __( 'field description (optional)', 'cmb2' ),
186
		'id'   => $prefix . 'textmoney',
187
		'type' => 'text_money',
188
		// 'before_field' => '£', // override '$' symbol if needed
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...
189
		// 'repeatable' => true,
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

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

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

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

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