|
1
|
|
|
<?php |
|
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 ( get_option( 'page_on_front' ) !== $cmb->object_id ) { |
|
34
|
|
|
return false; |
|
35
|
|
|
} |
|
36
|
|
|
return true; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Conditionally displays a field when used as a callback in the 'show_on_cb' field parameter |
|
41
|
|
|
* |
|
42
|
|
|
* @param CMB2_Field object $field Field object. |
|
43
|
|
|
* |
|
44
|
|
|
* @return bool True if metabox should show |
|
45
|
|
|
*/ |
|
46
|
|
|
function yourprefix_hide_if_no_cats( $field ) { |
|
47
|
|
|
// Don't show this field if not in the cats category. |
|
48
|
|
|
if ( ! has_tag( 'cats', $field->object_id ) ) { |
|
49
|
|
|
return false; |
|
50
|
|
|
} |
|
51
|
|
|
return true; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Manually render a field. |
|
56
|
|
|
* |
|
57
|
|
|
* @param array $field_args Array of field arguments. |
|
58
|
|
|
* @param CMB2_Field $field The field object. |
|
59
|
|
|
*/ |
|
60
|
|
|
function yourprefix_render_row_cb( $field_args, $field ) { |
|
|
|
|
|
|
61
|
|
|
$classes = $field->row_classes(); |
|
62
|
|
|
$id = $field->args( 'id' ); |
|
63
|
|
|
$label = $field->args( 'name' ); |
|
64
|
|
|
$name = $field->args( '_name' ); |
|
65
|
|
|
$value = $field->escaped_value(); |
|
66
|
|
|
$description = $field->args( 'description' ); |
|
67
|
|
|
?> |
|
68
|
|
|
<div class="custom-field-row <?php echo esc_attr( $classes ); ?>"> |
|
69
|
|
|
<p><label for="<?php echo esc_attr( $id ); ?>"><?php echo esc_html( $label ); ?></label></p> |
|
70
|
|
|
<p><input id="<?php echo esc_attr( $id ); ?>" type="text" name="<?php echo esc_attr( $name ); ?>" value="<?php echo $value; ?>"/></p> |
|
71
|
|
|
<p class="description"><?php echo esc_html( $description ); ?></p> |
|
72
|
|
|
</div> |
|
73
|
|
|
<?php |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Manually render a field column display. |
|
78
|
|
|
* |
|
79
|
|
|
* @param array $field_args Array of field arguments. |
|
80
|
|
|
* @param CMB2_Field $field The field object. |
|
81
|
|
|
*/ |
|
82
|
|
|
function yourprefix_display_text_small_column( $field_args, $field ) { |
|
|
|
|
|
|
83
|
|
|
?> |
|
84
|
|
|
<div class="custom-column-display <?php echo esc_attr( $field->row_classes() ); ?>"> |
|
85
|
|
|
<p><?php echo $field->escaped_value(); ?></p> |
|
86
|
|
|
<p class="description"><?php echo esc_html( $field->args( 'description' ) ); ?></p> |
|
87
|
|
|
</div> |
|
88
|
|
|
<?php |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Conditionally displays a message if the $post_id is 2 |
|
93
|
|
|
* |
|
94
|
|
|
* @param array $field_args Array of field parameters. |
|
95
|
|
|
* @param CMB2_Field object $field Field object. |
|
96
|
|
|
*/ |
|
97
|
|
|
function yourprefix_before_row_if_2( $field_args, $field ) { |
|
|
|
|
|
|
98
|
|
|
if ( 2 == $field->object_id ) { |
|
99
|
|
|
echo '<p>Testing <b>"before_row"</b> parameter (on $post_id 2)</p>'; |
|
100
|
|
|
} else { |
|
101
|
|
|
echo '<p>Testing <b>"before_row"</b> parameter (<b>NOT</b> on $post_id 2)</p>'; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
add_action( 'cmb2_admin_init', 'yourprefix_register_demo_metabox' ); |
|
106
|
|
|
/** |
|
107
|
|
|
* Hook in and add a demo metabox. Can only happen on the 'cmb2_admin_init' or 'cmb2_init' hook. |
|
108
|
|
|
*/ |
|
109
|
|
|
function yourprefix_register_demo_metabox() { |
|
110
|
|
|
$prefix = 'yourprefix_demo_'; |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Sample metabox to demonstrate each field type included |
|
114
|
|
|
*/ |
|
115
|
|
|
$cmb_demo = new_cmb2_box( array( |
|
116
|
|
|
'id' => $prefix . 'metabox', |
|
117
|
|
|
'title' => esc_html__( 'Test Metabox', 'cmb2' ), |
|
118
|
|
|
'object_types' => array( 'page' ), // Post type |
|
119
|
|
|
// 'show_on_cb' => 'yourprefix_show_if_front_page', // function should return a bool value |
|
|
|
|
|
|
120
|
|
|
// 'context' => 'normal', |
|
|
|
|
|
|
121
|
|
|
// 'priority' => 'high', |
|
|
|
|
|
|
122
|
|
|
// 'show_names' => true, // Show field names on the left |
|
|
|
|
|
|
123
|
|
|
// 'cmb_styles' => false, // false to disable the CMB stylesheet |
|
|
|
|
|
|
124
|
|
|
// 'closed' => true, // true to keep the metabox closed by default |
|
|
|
|
|
|
125
|
|
|
// 'classes' => 'extra-class', // Extra cmb2-wrap classes |
|
|
|
|
|
|
126
|
|
|
// 'classes_cb' => 'yourprefix_add_some_classes', // Add classes through a callback. |
|
|
|
|
|
|
127
|
|
|
) ); |
|
128
|
|
|
|
|
129
|
|
|
$cmb_demo->add_field( array( |
|
130
|
|
|
'name' => esc_html__( 'Test Text', 'cmb2' ), |
|
131
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
132
|
|
|
'id' => $prefix . 'text', |
|
133
|
|
|
'type' => 'text', |
|
134
|
|
|
'show_on_cb' => 'yourprefix_hide_if_no_cats', // function should return a bool value |
|
135
|
|
|
// 'sanitization_cb' => 'my_custom_sanitization', // custom sanitization callback parameter |
|
|
|
|
|
|
136
|
|
|
// 'escape_cb' => 'my_custom_escaping', // custom escaping callback parameter |
|
|
|
|
|
|
137
|
|
|
// 'on_front' => false, // Optionally designate a field to wp-admin only |
|
|
|
|
|
|
138
|
|
|
// 'repeatable' => true, |
|
|
|
|
|
|
139
|
|
|
// 'column' => true, // Display field value in the admin post-listing columns |
|
|
|
|
|
|
140
|
|
|
) ); |
|
141
|
|
|
|
|
142
|
|
|
$cmb_demo->add_field( array( |
|
143
|
|
|
'name' => esc_html__( 'Test Text Small', 'cmb2' ), |
|
144
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
145
|
|
|
'id' => $prefix . 'textsmall', |
|
146
|
|
|
'type' => 'text_small', |
|
147
|
|
|
// 'repeatable' => true, |
|
|
|
|
|
|
148
|
|
|
// 'column' => array( |
|
|
|
|
|
|
149
|
|
|
// 'name' => esc_html__( 'Column Title', 'cmb2' ), // Set the admin column title |
|
|
|
|
|
|
150
|
|
|
// 'position' => 2, // Set as the second column. |
|
|
|
|
|
|
151
|
|
|
// ); |
|
152
|
|
|
// 'display_cb' => 'yourprefix_display_text_small_column', // Output the display of the column values through a callback. |
|
|
|
|
|
|
153
|
|
|
) ); |
|
154
|
|
|
|
|
155
|
|
|
$cmb_demo->add_field( array( |
|
156
|
|
|
'name' => esc_html__( 'Test Text Medium', 'cmb2' ), |
|
157
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
158
|
|
|
'id' => $prefix . 'textmedium', |
|
159
|
|
|
'type' => 'text_medium', |
|
160
|
|
|
) ); |
|
161
|
|
|
|
|
162
|
|
|
$cmb_demo->add_field( array( |
|
163
|
|
|
'name' => esc_html__( 'Read-only Disabled Field', 'cmb2' ), |
|
164
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
165
|
|
|
'id' => $prefix . 'readonly', |
|
166
|
|
|
'type' => 'text_medium', |
|
167
|
|
|
'default' => esc_attr__( 'Hey there, I\'m a read-only field', 'cmb2' ), |
|
168
|
|
|
'save_field' => false, // Disables the saving of this field. |
|
169
|
|
|
'attributes' => array( |
|
170
|
|
|
'disabled' => 'disabled', |
|
171
|
|
|
'readonly' => 'readonly', |
|
172
|
|
|
), |
|
173
|
|
|
) ); |
|
174
|
|
|
|
|
175
|
|
|
$cmb_demo->add_field( array( |
|
176
|
|
|
'name' => esc_html__( 'Custom Rendered Field', 'cmb2' ), |
|
177
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
178
|
|
|
'id' => $prefix . 'render_row_cb', |
|
179
|
|
|
'type' => 'text', |
|
180
|
|
|
'render_row_cb' => 'yourprefix_render_row_cb', |
|
181
|
|
|
) ); |
|
182
|
|
|
|
|
183
|
|
|
$cmb_demo->add_field( array( |
|
184
|
|
|
'name' => esc_html__( 'Website URL', 'cmb2' ), |
|
185
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
186
|
|
|
'id' => $prefix . 'url', |
|
187
|
|
|
'type' => 'text_url', |
|
188
|
|
|
// 'protocols' => array('http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet'), // Array of allowed protocols |
|
|
|
|
|
|
189
|
|
|
// 'repeatable' => true, |
|
|
|
|
|
|
190
|
|
|
) ); |
|
191
|
|
|
|
|
192
|
|
|
$cmb_demo->add_field( array( |
|
193
|
|
|
'name' => esc_html__( 'Test Text Email', 'cmb2' ), |
|
194
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
195
|
|
|
'id' => $prefix . 'email', |
|
196
|
|
|
'type' => 'text_email', |
|
197
|
|
|
// 'repeatable' => true, |
|
|
|
|
|
|
198
|
|
|
) ); |
|
199
|
|
|
|
|
200
|
|
|
$cmb_demo->add_field( array( |
|
201
|
|
|
'name' => esc_html__( 'Test Time', 'cmb2' ), |
|
202
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
203
|
|
|
'id' => $prefix . 'time', |
|
204
|
|
|
'type' => 'text_time', |
|
205
|
|
|
// 'time_format' => 'H:i', // Set to 24hr format |
|
|
|
|
|
|
206
|
|
|
) ); |
|
207
|
|
|
|
|
208
|
|
|
$cmb_demo->add_field( array( |
|
209
|
|
|
'name' => esc_html__( 'Time zone', 'cmb2' ), |
|
210
|
|
|
'desc' => esc_html__( 'Time zone', 'cmb2' ), |
|
211
|
|
|
'id' => $prefix . 'timezone', |
|
212
|
|
|
'type' => 'select_timezone', |
|
213
|
|
|
) ); |
|
214
|
|
|
|
|
215
|
|
|
$cmb_demo->add_field( array( |
|
216
|
|
|
'name' => esc_html__( 'Test Date Picker', 'cmb2' ), |
|
217
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
218
|
|
|
'id' => $prefix . 'textdate', |
|
219
|
|
|
'type' => 'text_date', |
|
220
|
|
|
// 'date_format' => 'Y-m-d', |
|
|
|
|
|
|
221
|
|
|
) ); |
|
222
|
|
|
|
|
223
|
|
|
$cmb_demo->add_field( array( |
|
224
|
|
|
'name' => esc_html__( 'Test Date Picker (UNIX timestamp)', 'cmb2' ), |
|
225
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
226
|
|
|
'id' => $prefix . 'textdate_timestamp', |
|
227
|
|
|
'type' => 'text_date_timestamp', |
|
228
|
|
|
// 'timezone_meta_key' => $prefix . 'timezone', // Optionally make this field honor the timezone selected in the select_timezone specified above |
|
|
|
|
|
|
229
|
|
|
) ); |
|
230
|
|
|
|
|
231
|
|
|
$cmb_demo->add_field( array( |
|
232
|
|
|
'name' => esc_html__( 'Test Date/Time Picker Combo (UNIX timestamp)', 'cmb2' ), |
|
233
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
234
|
|
|
'id' => $prefix . 'datetime_timestamp', |
|
235
|
|
|
'type' => 'text_datetime_timestamp', |
|
236
|
|
|
) ); |
|
237
|
|
|
|
|
238
|
|
|
// This text_datetime_timestamp_timezone field type |
|
239
|
|
|
// is only compatible with PHP versions 5.3 or above. |
|
240
|
|
|
// Feel free to uncomment and use if your server meets the requirement |
|
241
|
|
|
// $cmb_demo->add_field( array( |
|
|
|
|
|
|
242
|
|
|
// 'name' => esc_html__( 'Test Date/Time Picker/Time zone Combo (serialized DateTime object)', 'cmb2' ), |
|
|
|
|
|
|
243
|
|
|
// 'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
|
|
|
|
|
244
|
|
|
// 'id' => $prefix . 'datetime_timestamp_timezone', |
|
|
|
|
|
|
245
|
|
|
// 'type' => 'text_datetime_timestamp_timezone', |
|
|
|
|
|
|
246
|
|
|
// ) ); |
|
247
|
|
|
|
|
248
|
|
|
$cmb_demo->add_field( array( |
|
249
|
|
|
'name' => esc_html__( 'Test Money', 'cmb2' ), |
|
250
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
251
|
|
|
'id' => $prefix . 'textmoney', |
|
252
|
|
|
'type' => 'text_money', |
|
253
|
|
|
// 'before_field' => '£', // override '$' symbol if needed |
|
|
|
|
|
|
254
|
|
|
// 'repeatable' => true, |
|
|
|
|
|
|
255
|
|
|
) ); |
|
256
|
|
|
|
|
257
|
|
|
$cmb_demo->add_field( array( |
|
258
|
|
|
'name' => esc_html__( 'Test Color Picker', 'cmb2' ), |
|
259
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
260
|
|
|
'id' => $prefix . 'colorpicker', |
|
261
|
|
|
'type' => 'colorpicker', |
|
262
|
|
|
'default' => '#ffffff', |
|
263
|
|
|
// 'attributes' => array( |
|
|
|
|
|
|
264
|
|
|
// 'data-colorpicker' => json_encode( array( |
|
|
|
|
|
|
265
|
|
|
// 'palettes' => array( '#3dd0cc', '#ff834c', '#4fa2c0', '#0bc991', ), |
|
|
|
|
|
|
266
|
|
|
// ) ), |
|
267
|
|
|
// ), |
|
268
|
|
|
) ); |
|
269
|
|
|
|
|
270
|
|
|
$cmb_demo->add_field( array( |
|
271
|
|
|
'name' => esc_html__( 'Test Text Area', 'cmb2' ), |
|
272
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
273
|
|
|
'id' => $prefix . 'textarea', |
|
274
|
|
|
'type' => 'textarea', |
|
275
|
|
|
) ); |
|
276
|
|
|
|
|
277
|
|
|
$cmb_demo->add_field( array( |
|
278
|
|
|
'name' => esc_html__( 'Test Text Area Small', 'cmb2' ), |
|
279
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
280
|
|
|
'id' => $prefix . 'textareasmall', |
|
281
|
|
|
'type' => 'textarea_small', |
|
282
|
|
|
) ); |
|
283
|
|
|
|
|
284
|
|
|
$cmb_demo->add_field( array( |
|
285
|
|
|
'name' => esc_html__( 'Test Text Area for Code', 'cmb2' ), |
|
286
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
287
|
|
|
'id' => $prefix . 'textarea_code', |
|
288
|
|
|
'type' => 'textarea_code', |
|
289
|
|
|
) ); |
|
290
|
|
|
|
|
291
|
|
|
$cmb_demo->add_field( array( |
|
292
|
|
|
'name' => esc_html__( 'Test Title Weeeee', 'cmb2' ), |
|
293
|
|
|
'desc' => esc_html__( 'This is a title description', 'cmb2' ), |
|
294
|
|
|
'id' => $prefix . 'title', |
|
295
|
|
|
'type' => 'title', |
|
296
|
|
|
) ); |
|
297
|
|
|
|
|
298
|
|
|
$cmb_demo->add_field( array( |
|
299
|
|
|
'name' => esc_html__( 'Test Select', 'cmb2' ), |
|
300
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
301
|
|
|
'id' => $prefix . 'select', |
|
302
|
|
|
'type' => 'select', |
|
303
|
|
|
'show_option_none' => true, |
|
304
|
|
|
'options' => array( |
|
305
|
|
|
'standard' => esc_html__( 'Option One', 'cmb2' ), |
|
306
|
|
|
'custom' => esc_html__( 'Option Two', 'cmb2' ), |
|
307
|
|
|
'none' => esc_html__( 'Option Three', 'cmb2' ), |
|
308
|
|
|
), |
|
309
|
|
|
) ); |
|
310
|
|
|
|
|
311
|
|
|
$cmb_demo->add_field( array( |
|
312
|
|
|
'name' => esc_html__( 'Test Radio inline', 'cmb2' ), |
|
313
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
314
|
|
|
'id' => $prefix . 'radio_inline', |
|
315
|
|
|
'type' => 'radio_inline', |
|
316
|
|
|
'show_option_none' => 'No Selection', |
|
317
|
|
|
'options' => array( |
|
318
|
|
|
'standard' => esc_html__( 'Option One', 'cmb2' ), |
|
319
|
|
|
'custom' => esc_html__( 'Option Two', 'cmb2' ), |
|
320
|
|
|
'none' => esc_html__( 'Option Three', 'cmb2' ), |
|
321
|
|
|
), |
|
322
|
|
|
) ); |
|
323
|
|
|
|
|
324
|
|
|
$cmb_demo->add_field( array( |
|
325
|
|
|
'name' => esc_html__( 'Test Radio', 'cmb2' ), |
|
326
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
327
|
|
|
'id' => $prefix . 'radio', |
|
328
|
|
|
'type' => 'radio', |
|
329
|
|
|
'options' => array( |
|
330
|
|
|
'option1' => esc_html__( 'Option One', 'cmb2' ), |
|
331
|
|
|
'option2' => esc_html__( 'Option Two', 'cmb2' ), |
|
332
|
|
|
'option3' => esc_html__( 'Option Three', 'cmb2' ), |
|
333
|
|
|
), |
|
334
|
|
|
) ); |
|
335
|
|
|
|
|
336
|
|
|
$cmb_demo->add_field( array( |
|
337
|
|
|
'name' => esc_html__( 'Test Taxonomy Radio', 'cmb2' ), |
|
338
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
339
|
|
|
'id' => $prefix . 'text_taxonomy_radio', |
|
340
|
|
|
'type' => 'taxonomy_radio', |
|
341
|
|
|
'taxonomy' => 'category', // Taxonomy Slug |
|
342
|
|
|
// 'inline' => true, // Toggles display to inline |
|
|
|
|
|
|
343
|
|
|
) ); |
|
344
|
|
|
|
|
345
|
|
|
$cmb_demo->add_field( array( |
|
346
|
|
|
'name' => esc_html__( 'Test Taxonomy Select', 'cmb2' ), |
|
347
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
348
|
|
|
'id' => $prefix . 'taxonomy_select', |
|
349
|
|
|
'type' => 'taxonomy_select', |
|
350
|
|
|
'taxonomy' => 'category', // Taxonomy Slug |
|
351
|
|
|
) ); |
|
352
|
|
|
|
|
353
|
|
|
$cmb_demo->add_field( array( |
|
354
|
|
|
'name' => esc_html__( 'Test Taxonomy Multi Checkbox', 'cmb2' ), |
|
355
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
356
|
|
|
'id' => $prefix . 'multitaxonomy', |
|
357
|
|
|
'type' => 'taxonomy_multicheck', |
|
358
|
|
|
'taxonomy' => 'post_tag', // Taxonomy Slug |
|
359
|
|
|
// 'inline' => true, // Toggles display to inline |
|
|
|
|
|
|
360
|
|
|
) ); |
|
361
|
|
|
|
|
362
|
|
|
$cmb_demo->add_field( array( |
|
363
|
|
|
'name' => esc_html__( 'Test Checkbox', 'cmb2' ), |
|
364
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
365
|
|
|
'id' => $prefix . 'checkbox', |
|
366
|
|
|
'type' => 'checkbox', |
|
367
|
|
|
) ); |
|
368
|
|
|
|
|
369
|
|
|
$cmb_demo->add_field( array( |
|
370
|
|
|
'name' => esc_html__( 'Test Multi Checkbox', 'cmb2' ), |
|
371
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
372
|
|
|
'id' => $prefix . 'multicheckbox', |
|
373
|
|
|
'type' => 'multicheck', |
|
374
|
|
|
// 'multiple' => true, // Store values in individual rows |
|
|
|
|
|
|
375
|
|
|
'options' => array( |
|
376
|
|
|
'check1' => esc_html__( 'Check One', 'cmb2' ), |
|
377
|
|
|
'check2' => esc_html__( 'Check Two', 'cmb2' ), |
|
378
|
|
|
'check3' => esc_html__( 'Check Three', 'cmb2' ), |
|
379
|
|
|
), |
|
380
|
|
|
// 'inline' => true, // Toggles display to inline |
|
|
|
|
|
|
381
|
|
|
) ); |
|
382
|
|
|
|
|
383
|
|
|
$cmb_demo->add_field( array( |
|
384
|
|
|
'name' => esc_html__( 'Test wysiwyg', 'cmb2' ), |
|
385
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
386
|
|
|
'id' => $prefix . 'wysiwyg', |
|
387
|
|
|
'type' => 'wysiwyg', |
|
388
|
|
|
'options' => array( |
|
389
|
|
|
'textarea_rows' => 5, |
|
390
|
|
|
), |
|
391
|
|
|
) ); |
|
392
|
|
|
|
|
393
|
|
|
$cmb_demo->add_field( array( |
|
394
|
|
|
'name' => esc_html__( 'Test Image', 'cmb2' ), |
|
395
|
|
|
'desc' => esc_html__( 'Upload an image or enter a URL.', 'cmb2' ), |
|
396
|
|
|
'id' => $prefix . 'image', |
|
397
|
|
|
'type' => 'file', |
|
398
|
|
|
) ); |
|
399
|
|
|
|
|
400
|
|
|
$cmb_demo->add_field( array( |
|
401
|
|
|
'name' => esc_html__( 'Multiple Files', 'cmb2' ), |
|
402
|
|
|
'desc' => esc_html__( 'Upload or add multiple images/attachments.', 'cmb2' ), |
|
403
|
|
|
'id' => $prefix . 'file_list', |
|
404
|
|
|
'type' => 'file_list', |
|
405
|
|
|
'preview_size' => array( 100, 100 ), // Default: array( 50, 50 ) |
|
|
|
|
|
|
406
|
|
|
) ); |
|
407
|
|
|
|
|
408
|
|
|
$cmb_demo->add_field( array( |
|
409
|
|
|
'name' => esc_html__( 'oEmbed', 'cmb2' ), |
|
410
|
|
|
'desc' => sprintf( |
|
411
|
|
|
/* translators: %s: link to codex.wordpress.org/Embeds */ |
|
412
|
|
|
esc_html__( 'Enter a youtube, twitter, or instagram URL. Supports services listed at %s.', 'cmb2' ), |
|
413
|
|
|
'<a href="https://codex.wordpress.org/Embeds">codex.wordpress.org/Embeds</a>' |
|
414
|
|
|
), |
|
415
|
|
|
'id' => $prefix . 'embed', |
|
416
|
|
|
'type' => 'oembed', |
|
417
|
|
|
) ); |
|
418
|
|
|
|
|
419
|
|
|
$cmb_demo->add_field( array( |
|
420
|
|
|
'name' => 'Testing Field Parameters', |
|
421
|
|
|
'id' => $prefix . 'parameters', |
|
422
|
|
|
'type' => 'text', |
|
423
|
|
|
'before_row' => 'yourprefix_before_row_if_2', // callback. |
|
424
|
|
|
'before' => '<p>Testing <b>"before"</b> parameter</p>', |
|
425
|
|
|
'before_field' => '<p>Testing <b>"before_field"</b> parameter</p>', |
|
426
|
|
|
'after_field' => '<p>Testing <b>"after_field"</b> parameter</p>', |
|
427
|
|
|
'after' => '<p>Testing <b>"after"</b> parameter</p>', |
|
428
|
|
|
'after_row' => '<p>Testing <b>"after_row"</b> parameter</p>', |
|
429
|
|
|
) ); |
|
430
|
|
|
|
|
431
|
|
|
} |
|
432
|
|
|
|
|
433
|
|
|
add_action( 'cmb2_admin_init', 'yourprefix_register_about_page_metabox' ); |
|
434
|
|
|
/** |
|
435
|
|
|
* Hook in and add a metabox that only appears on the 'About' page |
|
436
|
|
|
*/ |
|
437
|
|
|
function yourprefix_register_about_page_metabox() { |
|
438
|
|
|
$prefix = 'yourprefix_about_'; |
|
439
|
|
|
|
|
440
|
|
|
/** |
|
441
|
|
|
* Metabox to be displayed on a single page ID |
|
442
|
|
|
*/ |
|
443
|
|
|
$cmb_about_page = new_cmb2_box( array( |
|
444
|
|
|
'id' => $prefix . 'metabox', |
|
445
|
|
|
'title' => esc_html__( 'About Page Metabox', 'cmb2' ), |
|
446
|
|
|
'object_types' => array( 'page' ), // Post type |
|
447
|
|
|
'context' => 'normal', |
|
448
|
|
|
'priority' => 'high', |
|
449
|
|
|
'show_names' => true, // Show field names on the left |
|
450
|
|
|
'show_on' => array( |
|
451
|
|
|
'id' => array( 2 ), |
|
452
|
|
|
), // Specific post IDs to display this metabox |
|
453
|
|
|
) ); |
|
454
|
|
|
|
|
455
|
|
|
$cmb_about_page->add_field( array( |
|
456
|
|
|
'name' => esc_html__( 'Test Text', 'cmb2' ), |
|
457
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
458
|
|
|
'id' => $prefix . 'text', |
|
459
|
|
|
'type' => 'text', |
|
460
|
|
|
) ); |
|
461
|
|
|
|
|
462
|
|
|
} |
|
463
|
|
|
|
|
464
|
|
|
add_action( 'cmb2_admin_init', 'yourprefix_register_repeatable_group_field_metabox' ); |
|
465
|
|
|
/** |
|
466
|
|
|
* Hook in and add a metabox to demonstrate repeatable grouped fields |
|
467
|
|
|
*/ |
|
468
|
|
|
function yourprefix_register_repeatable_group_field_metabox() { |
|
469
|
|
|
$prefix = 'yourprefix_group_'; |
|
470
|
|
|
|
|
471
|
|
|
/** |
|
472
|
|
|
* Repeatable Field Groups |
|
473
|
|
|
*/ |
|
474
|
|
|
$cmb_group = new_cmb2_box( array( |
|
475
|
|
|
'id' => $prefix . 'metabox', |
|
476
|
|
|
'title' => esc_html__( 'Repeating Field Group', 'cmb2' ), |
|
477
|
|
|
'object_types' => array( 'page' ), |
|
478
|
|
|
) ); |
|
479
|
|
|
|
|
480
|
|
|
// $group_field_id is the field id string, so in this case: $prefix . 'demo' |
|
481
|
|
|
$group_field_id = $cmb_group->add_field( array( |
|
482
|
|
|
'id' => $prefix . 'demo', |
|
483
|
|
|
'type' => 'group', |
|
484
|
|
|
'description' => esc_html__( 'Generates reusable form entries', 'cmb2' ), |
|
485
|
|
|
'options' => array( |
|
486
|
|
|
'group_title' => esc_html__( 'Entry {#}', 'cmb2' ), // {#} gets replaced by row number |
|
487
|
|
|
'add_button' => esc_html__( 'Add Another Entry', 'cmb2' ), |
|
488
|
|
|
'remove_button' => esc_html__( 'Remove Entry', 'cmb2' ), |
|
489
|
|
|
'sortable' => true, // beta |
|
490
|
|
|
// 'closed' => true, // true to have the groups closed by default |
|
|
|
|
|
|
491
|
|
|
), |
|
492
|
|
|
) ); |
|
493
|
|
|
|
|
494
|
|
|
/** |
|
495
|
|
|
* Group fields works the same, except ids only need |
|
496
|
|
|
* to be unique to the group. Prefix is not needed. |
|
497
|
|
|
* |
|
498
|
|
|
* The parent field's id needs to be passed as the first argument. |
|
499
|
|
|
*/ |
|
500
|
|
|
$cmb_group->add_group_field( $group_field_id, array( |
|
501
|
|
|
'name' => esc_html__( 'Entry Title', 'cmb2' ), |
|
502
|
|
|
'id' => 'title', |
|
503
|
|
|
'type' => 'text', |
|
504
|
|
|
// 'repeatable' => true, // Repeatable fields are supported w/in repeatable groups (for most types) |
|
|
|
|
|
|
505
|
|
|
) ); |
|
506
|
|
|
|
|
507
|
|
|
$cmb_group->add_group_field( $group_field_id, array( |
|
508
|
|
|
'name' => esc_html__( 'Description', 'cmb2' ), |
|
509
|
|
|
'description' => esc_html__( 'Write a short description for this entry', 'cmb2' ), |
|
510
|
|
|
'id' => 'description', |
|
511
|
|
|
'type' => 'textarea_small', |
|
512
|
|
|
) ); |
|
513
|
|
|
|
|
514
|
|
|
$cmb_group->add_group_field( $group_field_id, array( |
|
515
|
|
|
'name' => esc_html__( 'Entry Image', 'cmb2' ), |
|
516
|
|
|
'id' => 'image', |
|
517
|
|
|
'type' => 'file', |
|
518
|
|
|
) ); |
|
519
|
|
|
|
|
520
|
|
|
$cmb_group->add_group_field( $group_field_id, array( |
|
521
|
|
|
'name' => esc_html__( 'Image Caption', 'cmb2' ), |
|
522
|
|
|
'id' => 'image_caption', |
|
523
|
|
|
'type' => 'text', |
|
524
|
|
|
) ); |
|
525
|
|
|
|
|
526
|
|
|
} |
|
527
|
|
|
|
|
528
|
|
|
add_action( 'cmb2_admin_init', 'yourprefix_register_user_profile_metabox' ); |
|
529
|
|
|
/** |
|
530
|
|
|
* Hook in and add a metabox to add fields to the user profile pages |
|
531
|
|
|
*/ |
|
532
|
|
|
function yourprefix_register_user_profile_metabox() { |
|
533
|
|
|
$prefix = 'yourprefix_user_'; |
|
534
|
|
|
|
|
535
|
|
|
/** |
|
536
|
|
|
* Metabox for the user profile screen |
|
537
|
|
|
*/ |
|
538
|
|
|
$cmb_user = new_cmb2_box( array( |
|
539
|
|
|
'id' => $prefix . 'edit', |
|
540
|
|
|
'title' => esc_html__( 'User Profile Metabox', 'cmb2' ), // Doesn't output for user boxes |
|
541
|
|
|
'object_types' => array( 'user' ), // Tells CMB2 to use user_meta vs post_meta |
|
542
|
|
|
'show_names' => true, |
|
543
|
|
|
'new_user_section' => 'add-new-user', // where form will show on new user page. 'add-existing-user' is only other valid option. |
|
544
|
|
|
) ); |
|
545
|
|
|
|
|
546
|
|
|
$cmb_user->add_field( array( |
|
547
|
|
|
'name' => esc_html__( 'Extra Info', 'cmb2' ), |
|
548
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
549
|
|
|
'id' => $prefix . 'extra_info', |
|
550
|
|
|
'type' => 'title', |
|
551
|
|
|
'on_front' => false, |
|
552
|
|
|
) ); |
|
553
|
|
|
|
|
554
|
|
|
$cmb_user->add_field( array( |
|
555
|
|
|
'name' => esc_html__( 'Avatar', 'cmb2' ), |
|
556
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
557
|
|
|
'id' => $prefix . 'avatar', |
|
558
|
|
|
'type' => 'file', |
|
559
|
|
|
) ); |
|
560
|
|
|
|
|
561
|
|
|
$cmb_user->add_field( array( |
|
562
|
|
|
'name' => esc_html__( 'Facebook URL', 'cmb2' ), |
|
563
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
564
|
|
|
'id' => $prefix . 'facebookurl', |
|
565
|
|
|
'type' => 'text_url', |
|
566
|
|
|
) ); |
|
567
|
|
|
|
|
568
|
|
|
$cmb_user->add_field( array( |
|
569
|
|
|
'name' => esc_html__( 'Twitter URL', 'cmb2' ), |
|
570
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
571
|
|
|
'id' => $prefix . 'twitterurl', |
|
572
|
|
|
'type' => 'text_url', |
|
573
|
|
|
) ); |
|
574
|
|
|
|
|
575
|
|
|
$cmb_user->add_field( array( |
|
576
|
|
|
'name' => esc_html__( 'Google+ URL', 'cmb2' ), |
|
577
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
578
|
|
|
'id' => $prefix . 'googleplusurl', |
|
579
|
|
|
'type' => 'text_url', |
|
580
|
|
|
) ); |
|
581
|
|
|
|
|
582
|
|
|
$cmb_user->add_field( array( |
|
583
|
|
|
'name' => esc_html__( 'Linkedin URL', 'cmb2' ), |
|
584
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
585
|
|
|
'id' => $prefix . 'linkedinurl', |
|
586
|
|
|
'type' => 'text_url', |
|
587
|
|
|
) ); |
|
588
|
|
|
|
|
589
|
|
|
$cmb_user->add_field( array( |
|
590
|
|
|
'name' => esc_html__( 'User Field', 'cmb2' ), |
|
591
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
592
|
|
|
'id' => $prefix . 'user_text_field', |
|
593
|
|
|
'type' => 'text', |
|
594
|
|
|
) ); |
|
595
|
|
|
|
|
596
|
|
|
} |
|
597
|
|
|
|
|
598
|
|
|
add_action( 'cmb2_admin_init', 'yourprefix_register_taxonomy_metabox' ); |
|
599
|
|
|
/** |
|
600
|
|
|
* Hook in and add a metabox to add fields to taxonomy terms |
|
601
|
|
|
*/ |
|
602
|
|
|
function yourprefix_register_taxonomy_metabox() { |
|
603
|
|
|
$prefix = 'yourprefix_term_'; |
|
604
|
|
|
|
|
605
|
|
|
/** |
|
606
|
|
|
* Metabox to add fields to categories and tags |
|
607
|
|
|
*/ |
|
608
|
|
|
$cmb_term = new_cmb2_box( array( |
|
609
|
|
|
'id' => $prefix . 'edit', |
|
610
|
|
|
'title' => esc_html__( 'Category Metabox', 'cmb2' ), // Doesn't output for term boxes |
|
611
|
|
|
'object_types' => array( 'term' ), // Tells CMB2 to use term_meta vs post_meta |
|
612
|
|
|
'taxonomies' => array( 'category', 'post_tag' ), // Tells CMB2 which taxonomies should have these fields |
|
613
|
|
|
// 'new_term_section' => true, // Will display in the "Add New Category" section |
|
|
|
|
|
|
614
|
|
|
) ); |
|
615
|
|
|
|
|
616
|
|
|
$cmb_term->add_field( array( |
|
617
|
|
|
'name' => esc_html__( 'Extra Info', 'cmb2' ), |
|
618
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
619
|
|
|
'id' => $prefix . 'extra_info', |
|
620
|
|
|
'type' => 'title', |
|
621
|
|
|
'on_front' => false, |
|
622
|
|
|
) ); |
|
623
|
|
|
|
|
624
|
|
|
$cmb_term->add_field( array( |
|
625
|
|
|
'name' => esc_html__( 'Term Image', 'cmb2' ), |
|
626
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
627
|
|
|
'id' => $prefix . 'avatar', |
|
628
|
|
|
'type' => 'file', |
|
629
|
|
|
) ); |
|
630
|
|
|
|
|
631
|
|
|
$cmb_term->add_field( array( |
|
632
|
|
|
'name' => esc_html__( 'Arbitrary Term Field', 'cmb2' ), |
|
633
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
634
|
|
|
'id' => $prefix . 'term_text_field', |
|
635
|
|
|
'type' => 'text', |
|
636
|
|
|
) ); |
|
637
|
|
|
|
|
638
|
|
|
} |
|
639
|
|
|
|
|
640
|
|
|
add_action( 'cmb2_admin_init', 'yourprefix_register_theme_options_metabox' ); |
|
641
|
|
|
/** |
|
642
|
|
|
* Hook in and register a metabox to handle a theme options page |
|
643
|
|
|
*/ |
|
644
|
|
|
function yourprefix_register_theme_options_metabox() { |
|
645
|
|
|
|
|
646
|
|
|
$option_key = 'yourprefix_theme_options'; |
|
647
|
|
|
|
|
648
|
|
|
/** |
|
649
|
|
|
* Metabox for an options page. Will not be added automatically, but needs to be called with |
|
650
|
|
|
* the `cmb2_metabox_form` helper function. See https://github.com/WebDevStudios/CMB2/wiki for more info. |
|
651
|
|
|
*/ |
|
652
|
|
|
$cmb_options = new_cmb2_box( array( |
|
653
|
|
|
'id' => $option_key . 'page', |
|
654
|
|
|
'title' => esc_html__( 'Theme Options Metabox', 'cmb2' ), |
|
655
|
|
|
'hookup' => false, // Do not need the normal user/post hookup. |
|
656
|
|
|
'show_on' => array( |
|
657
|
|
|
// These are important, don't remove. |
|
658
|
|
|
'key' => 'options-page', |
|
659
|
|
|
'value' => array( $option_key ), |
|
660
|
|
|
), |
|
661
|
|
|
) ); |
|
662
|
|
|
|
|
663
|
|
|
/** |
|
664
|
|
|
* Options fields ids only need |
|
665
|
|
|
* to be unique within this option group. |
|
666
|
|
|
* Prefix is not needed. |
|
667
|
|
|
*/ |
|
668
|
|
|
$cmb_options->add_field( array( |
|
669
|
|
|
'name' => esc_html__( 'Site Background Color', 'cmb2' ), |
|
670
|
|
|
'desc' => esc_html__( 'field description (optional)', 'cmb2' ), |
|
671
|
|
|
'id' => 'bg_color', |
|
672
|
|
|
'type' => 'colorpicker', |
|
673
|
|
|
'default' => '#ffffff', |
|
674
|
|
|
) ); |
|
675
|
|
|
|
|
676
|
|
|
} |
|
677
|
|
|
|
|
678
|
|
|
/** |
|
679
|
|
|
* Only show this box in the CMB2 REST API if the user is logged in. |
|
680
|
|
|
* |
|
681
|
|
|
* @param bool $is_allowed Whether this box and its fields are allowed to be viewed. |
|
682
|
|
|
* @param CMB2_REST_Controller $cmb_controller The controller object. |
|
683
|
|
|
* CMB2 object available via `$cmb_controller->rest_box->cmb`. |
|
684
|
|
|
* |
|
685
|
|
|
* @return bool Whether this box and its fields are allowed to be viewed. |
|
686
|
|
|
*/ |
|
687
|
|
|
function yourprefix_limit_rest_view_to_logged_in_users( $is_allowed, $cmb_controller ) { |
|
|
|
|
|
|
688
|
|
|
if ( ! is_user_logged_in() ) { |
|
689
|
|
|
$is_allowed = false; |
|
690
|
|
|
} |
|
691
|
|
|
|
|
692
|
|
|
return $is_allowed; |
|
693
|
|
|
} |
|
694
|
|
|
|
|
695
|
|
|
add_action( 'cmb2_init', 'yourprefix_register_rest_api_box' ); |
|
696
|
|
|
/** |
|
697
|
|
|
* Hook in and add a box to be available in the CMB2 REST API. Can only happen on the 'cmb2_init' hook. |
|
698
|
|
|
* More info: https://github.com/WebDevStudios/CMB2/wiki/REST-API |
|
699
|
|
|
*/ |
|
700
|
|
|
function yourprefix_register_rest_api_box() { |
|
701
|
|
|
$prefix = 'yourprefix_rest_'; |
|
702
|
|
|
|
|
703
|
|
|
$cmb_rest = new_cmb2_box( array( |
|
704
|
|
|
'id' => $prefix . 'metabox', |
|
705
|
|
|
'title' => esc_html__( 'REST Test Box', 'cmb2' ), |
|
706
|
|
|
'object_types' => array( 'page' ), // Post type |
|
707
|
|
|
'show_in_rest' => WP_REST_Server::ALLMETHODS, // WP_REST_Server::READABLE|WP_REST_Server::EDITABLE, // Determines which HTTP methods the box is visible in. |
|
|
|
|
|
|
708
|
|
|
// Optional callback to limit box visibility. |
|
709
|
|
|
// See: https://github.com/WebDevStudios/CMB2/wiki/REST-API#permissions |
|
710
|
|
|
// 'get_box_permissions_check_cb' => 'yourprefix_limit_rest_view_to_logged_in_users', |
|
|
|
|
|
|
711
|
|
|
) ); |
|
712
|
|
|
|
|
713
|
|
|
$cmb_rest->add_field( array( |
|
714
|
|
|
'name' => esc_html__( 'REST Test Text', 'cmb2' ), |
|
715
|
|
|
'desc' => esc_html__( 'Will show in the REST API for this box and for pages.', 'cmb2' ), |
|
716
|
|
|
'id' => $prefix . 'text', |
|
717
|
|
|
'type' => 'text', |
|
718
|
|
|
) ); |
|
719
|
|
|
|
|
720
|
|
|
$cmb_rest->add_field( array( |
|
721
|
|
|
'name' => esc_html__( 'REST Editable Test Text', 'cmb2' ), |
|
722
|
|
|
'desc' => esc_html__( 'Will show in REST API "editable" contexts only (`POST` requests).', 'cmb2' ), |
|
723
|
|
|
'id' => $prefix . 'editable_text', |
|
724
|
|
|
'type' => 'text', |
|
725
|
|
|
'show_in_rest' => WP_REST_Server::EDITABLE,// WP_REST_Server::ALLMETHODS|WP_REST_Server::READABLE, // Determines which HTTP methods the field is visible in. Will override the cmb2_box 'show_in_rest' param. |
|
|
|
|
|
|
726
|
|
|
) ); |
|
727
|
|
|
} |
|
728
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.