-
Notifications
You must be signed in to change notification settings - Fork 123
/
example-functions.php
454 lines (390 loc) · 9.74 KB
/
example-functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<?php
/**
* Example functions to reference for developers.
*
* @package WordPress
* @subpackage Custom Meta Boxes
*/
/**
* Define the metabox and field configurations.
*
* @param array $meta_boxes Existing metaboxes.
* @return array
*/
function cmb_sample_metaboxes( array $meta_boxes ) {
/**
* Example of all available fields.
*/
$fields = array(
/**
* Basic Text Field.
*/
array(
'id' => 'field-text',
'name' => 'Text input field',
'type' => 'text',
),
/**
* Text Field with all options.
*/
array(
// Required. ID of CMB value to be inserted into database.
'id' => 'field-2',
// Required.Type of field to call.
'type' => 'text',
// Optional. Title that will show above field.
'name' => __( 'Name', 'cmb' ),
// Optional. Contextual information. Will display underneath field.
'desc' => __( 'Repeatable', 'cmb' ),
// Optional. Whether field should be repeatable.
'repeatable' => true,
// Optional. Maximum number of repetitions allowed.
'repeatable_max' => 5,
// Optional. Whether repeated instances should be sortable.
'sortable' => true,
// Optional. Don't display field label if set to false.
'show_label' => false,
// Optional. Make a filed uneditable by user but value will still be saved.
'readonly' => false,
// Optional. Make field uneditable by user and value will NOT be saved.
'disabled' => false,
// Optional. Default value for new posts.
'default' => '',
// Optional. Number of columns field should take up out of 12.
'cols' => '12',
// Optional. Add custom styles to field.
'style' => '',
// Optional. Add custom CSS classes to field.
'class' => '',
// Optional. Custom data callback for field.
'data_delegate' => null,
// Optional. Custom save method for field.
'save_callback' => null,
),
/**
* Small Text Field.
*/
array(
'id' => 'field-small-text',
'name' => 'Small text input field',
'type' => 'text_small',
),
/**
* Single Checkbox Field.
*/
array(
'id' => 'field-checkbox',
'name' => 'Checkbox field',
'type' => 'checkbox',
),
/**
* Checkbox Multi Field.
*/
array(
'id' => 'field-7b',
'name' => 'Checkbox-Multi field',
'type' => 'checkbox_multi',
'default' => array( 'option1' ),
'options' => array(
'option0' => 'Option with a really long label',
'option1' => 'Option 1',
'option2' => 'Option 2',
'option3' => 'Option 3',
'option4' => 'Option 4',
)
),
/**
* Colorpicker Field.
*/
array(
'id' => 'field-colorpicker',
'name' => 'Color',
'type' => 'colorpicker',
),
/**
* Basic Date Field.
*/
array(
'id' => 'field-date',
'name' => 'Date input field',
'type' => 'date',
),
/**
* Basic Time Field.
*/
array(
'id' => 'field-time',
'name' => 'Time input field',
'type' => 'time',
),
/**
* Date UNIX Field.
*/
array(
'id' => 'field-date-unix',
'name' => 'Date (unix) input field',
'type' => 'date_unix',
),
/**
* Date & Time UNIX Field.
*/
array(
'id' => 'field-datetime-unix',
'name' => 'Date & Time (unix) input field',
'type' => 'datetime_unix',
),
/**
* Email Field.
*/
array(
'id' => 'field-email',
'name' => 'Email field',
'type' => 'email',
),
/*
* Google Map Field.
*/
array(
'id' => 'field-gmap',
'name' => 'Location',
'type' => 'gmap',
// Required. Pass a Google Maps API key. Alternatively, set with CMB_GAPI_KEY constant.
'google_api_key' => '{CUSTOM_KEY}',
// Optional. Width of address input + map display.
'field_width' => '100%',
// Optional. Static height of address input + map display.
'field_height' => '250px',
// Optional Default latitude for map to display on page load.
'default_lat' => '51.5073509',
// Optional. Default longitude for map to display on page load.
'default_long' => '-0.12775829999998223',
// Optional. Default zoom for map to display on page load. 1 = Whole world, 20 = Individual buildings
'default_zoom' => '8',
// Optional. Override Marker title text.
'string-marker-title' => esc_html__( 'Drag to set the exact location', 'cmb' ),
// Optional. Override maps error message.
'string-gmaps-api-not-loaded' => esc_html__( 'Google Maps API not loaded.', 'cmb' ),
),
/**
* Radio Input Field.
*/
array(
'id' => 'field-radio',
'name' => 'Radio input field',
'type' => 'radio',
'options' => array( // Options for individual radio inputs.
'Option 1',
'Option 2',
),
),
/**
* File Upload Field.
*/
array(
'id' => 'field-file',
'name' => 'File field',
'type' => 'file',
'library-type' => array( // Optional. Type of media allowed [ 'audio', 'video', 'text', 'application' ].
'video',
),
),
/**
* File Image Upload Field.
*/
array(
'id' => 'field-image',
'name' => 'Image upload field',
'type' => 'image',
'repeatable' => true,
'size' => 'thumbnail', // Optional. Registered media size to display.
'show_size' => true, // Optional. Whether to show the image dimensions underneath image itself.
),
/**
* Hidden Field.
*/
array(
'id' => 'field-hidden',
'name' => 'Hidden field',
'type' => 'hidden',
'default' => 'hidden default value',
),
/*
* Select Field.
*/
array(
'id' => 'field-select',
'name' => 'Select field',
'type' => 'select',
'options' => array(
'option-1' => 'Option 1',
'option-2' => 'Option 2',
'option-3' => 'Option 3',
),
'allow_none' => true, // Optional. Allow a user to not select any options.
'multiple' => true, // Optional. Allow multi-select.
'select2_options' => array(), // Optional. Array of options to pass to Select2.
),
/**
* Select for Taxonomies Field.
*/
array(
'id' => 'field-select-for-taxonomies',
'name' => 'Select taxonomy field',
'type' => 'taxonomy_select',
'taxonomy' => 'category', // Required. Name of taxonomy to pull options from.
'hide_empty' => false, // Optional. Whether to hide empty terms or not.
'multiple' => true, // Optional. Allow multi-select.
),
/**
* Select for Posts Field.
*/
array(
'id' => 'field-select-for-posts',
'name' => 'Post select field',
'type' => 'post_select',
'use_ajax' => false, // Optional. Use AJAX for instant results or load on pageload.
'multiple' => true, // Optional. Allow multi-select.
'query' => array( // Optional. WP_Query options to pass through.
'cat' => 1,
),
),
/**
* Plain Title Field.
*/
array(
'id' => 'field-title',
'name' => 'Title Field',
'type' => 'title',
),
/**
* Textarea Field.
*/
array(
'id' => 'field-textarea',
'name' => 'Textarea field',
'type' => 'textarea',
),
/**
* Textarea Code Field.
*/
array(
'id' => 'field-textarea-code',
'name' => 'Code textarea field',
'type' => 'textarea_code',
),
/**
* WYSIWYG (What You See is What You Get) TinyMCE Field.
*/
array(
'id' => 'field-wysiwyg',
'name' => 'WYSIWYG field',
'type' => 'wysiwyg',
'options' => array( // Options to pass into TinyMCE instance.
'editor_height' => '100',
),
),
/**
* URL Field.
*/
array(
'id' => 'field-url',
'name' => 'URL field',
'type' => 'url',
),
);
/**
* Metabox instantiation.
*/
$meta_boxes[] = array(
'title' => 'CMB Test - all fields',
'pages' => 'post',
'fields' => $fields,
);
/**
* Examples of Groups and Columns.
*/
$groups_and_cols = array(
array(
'id' => 'gac-1',
'name' => 'Text input field',
'type' => 'text',
'cols' => 4,
),
array(
'id' => 'gac-2',
'name' => 'Text input field',
'type' => 'text',
'cols' => 4,
),
array(
'id' => 'gac-3',
'name' => 'Text input field',
'type' => 'text',
'cols' => 4,
),
/**
* Group within a Group.
*/
array(
'id' => 'gac-4',
'name' => 'Group (4 columns)',
'type' => 'group',
'cols' => 4,
'fields' => array(
array(
'id' => 'gac-4-f-1',
'name' => 'Textarea field',
'type' => 'textarea',
),
),
),
array(
'id' => 'gac-5',
'name' => 'Group (8 columns)',
'type' => 'group',
'cols' => 8,
'fields' => array(
array(
'id' => 'gac-4-f-1',
'name' => 'Text input field',
'type' => 'text',
),
array(
'id' => 'gac-4-f-2',
'name' => 'Text input field',
'type' => 'text',
),
),
),
);
$meta_boxes[] = array(
'title' => 'Groups and Columns',
'pages' => 'post',
'fields' => $groups_and_cols,
);
/**
* Example of repeatable group. Using all fields.
* For this example, copy fields from $fields, update ID.
*/
$group_fields = $fields;
foreach ( $group_fields as &$field ) {
$field['id'] = str_replace( 'field', 'gfield', $field['id'] );
}
$meta_boxes[] = array(
'title' => 'CMB Test - group (all fields)',
'pages' => 'post',
'fields' => array(
array(
'id' => 'gp',
'name' => 'My Repeatable Group',
'type' => 'group',
'repeatable' => true,
'sortable' => true,
'fields' => $group_fields,
'desc' => 'This is the group description.',
),
),
);
return $meta_boxes;
}
add_filter( 'cmb_meta_boxes', 'cmb_sample_metaboxes' );