Completed
Pull Request — trunk (#541)
by Justin
14:11 queued 01:21
created

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