-
Notifications
You must be signed in to change notification settings - Fork 9
/
custom-meta-boxes.sublime-snippet
365 lines (345 loc) · 12 KB
/
custom-meta-boxes.sublime-snippet
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
<snippet>
<content><![CDATA[
// Add the Meta Box
function add_${1:custom_meta}_box() {
add_meta_box(
'${1:custom_meta}_box', // \$id
'${2:Custom Meta Box}', // \$title
'show_${1:custom_meta}_box', // \$callback
'post', // \$page
'normal', // \$context
'high'); // \$priority
}
add_action('add_meta_boxes', 'add_${1:custom_meta}_box');
// Field Array
\$prefix = 'custom_';
\$${1:custom_meta}_fields = array(
array(
'label' => 'Text Input',
'desc' => 'A description for the field.',
'id' => \$prefix.'text',
'type' => 'text'
),
array(
'label' => 'Textarea',
'desc' => 'A description for the field.',
'id' => \$prefix.'textarea',
'type' => 'textarea'
),
array(
'label' => 'Checkbox Input',
'desc' => 'A description for the field.',
'id' => \$prefix.'checkbox',
'type' => 'checkbox'
),
array(
'label' => 'Select Box',
'desc' => 'A description for the field.',
'id' => \$prefix.'select',
'type' => 'select',
'options' => array (
'one' => array (
'label' => 'Option One',
'value' => 'one'
),
'two' => array (
'label' => 'Option Two',
'value' => 'two'
),
'three' => array (
'label' => 'Option Three',
'value' => 'three'
)
)
),
array (
'label' => 'Radio Group',
'desc' => 'A description for the field.',
'id' => \$prefix.'radio',
'type' => 'radio',
'options' => array (
'one' => array (
'label' => 'Option One',
'value' => 'one'
),
'two' => array (
'label' => 'Option Two',
'value' => 'two'
),
'three' => array (
'label' => 'Option Three',
'value' => 'three'
)
)
),
array (
'label' => 'Checkbox Group',
'desc' => 'A description for the field.',
'id' => \$prefix.'checkbox_group',
'type' => 'checkbox_group',
'options' => array (
'one' => array (
'label' => 'Option One',
'value' => 'one'
),
'two' => array (
'label' => 'Option Two',
'value' => 'two'
),
'three' => array (
'label' => 'Option Three',
'value' => 'three'
)
)
),
array(
'label' => 'Category',
'id' => 'category',
'type' => 'tax_select'
),
array(
'label' => 'Post List',
'desc' => 'A description for the field.',
'id' => \$prefix.'post_id',
'type' => 'post_list',
'post_type' => array('post','page')
),
array(
'label' => 'Date',
'desc' => 'A description for the field.',
'id' => \$prefix.'date',
'type' => 'date'
),
array(
'label' => 'Slider',
'desc' => 'A description for the field.',
'id' => \$prefix.'slider',
'type' => 'slider',
'min' => '0',
'max' => '100',
'step' => '5'
),
array(
'label' => 'Image',
'desc' => 'A description for the field.',
'id' => \$prefix.'image',
'type' => 'image'
),
array(
'label' => 'Repeatable',
'desc' => 'A description for the field.',
'id' => \$prefix.'repeatable',
'type' => 'repeatable'
)
);
// enqueue scripts and styles, but only if is_admin
if(is_admin()) {
wp_enqueue_script('jquery-ui-datepicker');
wp_enqueue_script('jquery-ui-slider');
wp_enqueue_script('custom-js', get_template_directory_uri().'/js/custom-js.js');
wp_enqueue_style('jquery-ui-custom', get_template_directory_uri().'/css/jquery-ui-custom.css');
}
// add some custom js to the head of the page
add_action('admin_head','add_custom_scripts');
function add_custom_scripts() {
global \$${1:custom_meta}_fields, \$post;
\$output = '<script type="text/javascript">
jQuery(function() {';
foreach (\$${1:custom_meta}_fields as \$field) { // loop through the fields looking for certain types
// date
if(\$field['type'] == 'date')
\$output .= 'jQuery(".datepicker").datepicker();';
// slider
if (\$field['type'] == 'slider') {
\$value = get_post_meta(\$post->ID, \$field['id'], true);
if (\$value == '') \$value = \$field['min'];
\$output .= '
jQuery( "#'.\$field['id'].'-slider" ).slider({
value: '.\$value.',
min: '.\$field['min'].',
max: '.\$field['max'].',
step: '.\$field['step'].',
slide: function( event, ui ) {
jQuery( "#'.\$field['id'].'" ).val( ui.value );
}
});';
}
}
\$output .= '});
</script>';
echo \$output;
}
// The Callback
function show_${1:custom_meta}_box() {
global \$${1:custom_meta}_fields, \$post;
// Use nonce for verification
echo '<input type="hidden" name="${1:custom_meta}_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
// Begin the field table and loop
echo '<table class="form-table">';
foreach (\$${1:custom_meta}_fields as \$field) {
// get value of this field if it exists for this post
\$meta = get_post_meta(\$post->ID, \$field['id'], true);
// begin a table row with
echo '<tr>
<th><label for="'.\$field['id'].'">'.\$field['label'].'</label></th>
<td>';
switch(\$field['type']) {
// text
case 'text':
echo '<input type="text" name="'.\$field['id'].'" id="'.\$field['id'].'" value="'.\$meta.'" size="30" />
<br /><span class="description">'.\$field['desc'].'</span>';
break;
// textarea
case 'textarea':
echo '<textarea name="'.\$field['id'].'" id="'.\$field['id'].'" cols="60" rows="4">'.\$meta.'</textarea>
<br /><span class="description">'.\$field['desc'].'</span>';
break;
// checkbox
case 'checkbox':
echo '<input type="checkbox" name="'.\$field['id'].'" id="'.\$field['id'].'" ',\$meta ? ' checked="checked"' : '','/>
<label for="'.\$field['id'].'">'.\$field['desc'].'</label>';
break;
// select
case 'select':
echo '<select name="'.\$field['id'].'" id="'.\$field['id'].'">';
foreach (\$field['options'] as \$option) {
echo '<option', \$meta == \$option['value'] ? ' selected="selected"' : '', ' value="'.\$option['value'].'">'.\$option['label'].'</option>';
}
echo '</select><br /><span class="description">'.\$field['desc'].'</span>';
break;
// radio
case 'radio':
foreach ( \$field['options'] as \$option ) {
echo '<input type="radio" name="'.\$field['id'].'" id="'.\$option['value'].'" value="'.\$option['value'].'" ',\$meta == \$option['value'] ? ' checked="checked"' : '',' />
<label for="'.\$option['value'].'">'.\$option['label'].'</label><br />';
}
echo '<span class="description">'.\$field['desc'].'</span>';
break;
// checkbox_group
case 'checkbox_group':
foreach (\$field['options'] as \$option) {
echo '<input type="checkbox" value="'.\$option['value'].'" name="'.\$field['id'].'[]" id="'.\$option['value'].'"',\$meta && in_array(\$option['value'], \$meta) ? ' checked="checked"' : '',' />
<label for="'.\$option['value'].'">'.\$option['label'].'</label><br />';
}
echo '<span class="description">'.\$field['desc'].'</span>';
break;
// tax_select
case 'tax_select':
echo '<select name="'.\$field['id'].'" id="'.\$field['id'].'">
<option value="">Select One</option>'; // Select One
\$terms = get_terms(\$field['id'], 'get=all');
\$selected = wp_get_object_terms(\$post->ID, \$field['id']);
foreach (\$terms as \$term) {
if (!empty(\$selected) && !strcmp(\$term->slug, \$selected[0]->slug))
echo '<option value="'.\$term->slug.'" selected="selected">'.\$term->name.'</option>';
else
echo '<option value="'.\$term->slug.'">'.\$term->name.'</option>';
}
\$taxonomy = get_taxonomy(\$field['id']);
echo '</select><br /><span class="description"><a href="'.get_bloginfo('home').'/wp-admin/edit-tags.php?taxonomy='.\$field['id'].'">Manage '.\$taxonomy->label.'</a></span>';
break;
// post_list
case 'post_list':
\$items = get_posts( array (
'post_type' => \$field['post_type'],
'posts_per_page' => -1
));
echo '<select name="'.\$field['id'].'" id="'.\$field['id'].'">
<option value="">Select One</option>'; // Select One
foreach(\$items as \$item) {
echo '<option value="'.\$item->ID.'"',\$meta == \$item->ID ? ' selected="selected"' : '','>'.\$item->post_type.': '.\$item->post_title.'</option>';
} // end foreach
echo '</select><br /><span class="description">'.\$field['desc'].'</span>';
break;
// date
case 'date':
echo '<input type="text" class="datepicker" name="'.\$field['id'].'" id="'.\$field['id'].'" value="'.\$meta.'" size="30" />
<br /><span class="description">'.\$field['desc'].'</span>';
break;
// slider
case 'slider':
\$value = \$meta != '' ? \$meta : '0';
echo '<div id="'.\$field['id'].'-slider"></div>
<input type="text" name="'.\$field['id'].'" id="'.\$field['id'].'" value="'.\$value.'" size="5" />
<br /><span class="description">'.\$field['desc'].'</span>';
break;
// image
case 'image':
\$image = get_template_directory_uri().'/images/image.png';
echo '<span class="custom_default_image" style="display:none">'.\$image.'</span>';
if (\$meta) { \$image = wp_get_attachment_image_src(\$meta, 'medium'); \$image = \$image[0]; }
echo '<input name="'.\$field['id'].'" type="hidden" class="custom_upload_image" value="'.\$meta.'" />
<img src="'.\$image.'" class="custom_preview_image" alt="" /><br />
<input class="custom_upload_image_button button" type="button" value="Choose Image" />
<small> <a href="#" class="custom_clear_image_button">Remove Image</a></small>
<br clear="all" /><span class="description">'.\$field['desc'].'</span>';
break;
// repeatable
case 'repeatable':
echo '<a class="repeatable-add button" href="#">+</a>
<ul id="'.\$field['id'].'-repeatable" class="custom_repeatable">';
\$i = 0;
if (\$meta) {
foreach(\$meta as \$row) {
echo '<li><span class="sort hndle">|||</span>
<input type="text" name="'.\$field['id'].'['.\$i.']" id="'.\$field['id'].'" value="'.\$row.'" size="30" />
<a class="repeatable-remove button" href="#">-</a></li>';
\$i++;
}
} else {
echo '<li><span class="sort hndle">|||</span>
<input type="text" name="'.\$field['id'].'['.\$i.']" id="'.\$field['id'].'" value="" size="30" />
<a class="repeatable-remove button" href="#">-</a></li>';
}
echo '</ul>
<span class="description">'.\$field['desc'].'</span>';
break;
} //end switch
echo '</td></tr>';
} // end foreach
echo '</table>'; // end table
}
function ${1:custom_meta}_remove_taxonomy_boxes() {
remove_meta_box('categorydiv', 'post', 'side');
}
add_action( 'admin_menu' , '${1:custom_meta}_remove_taxonomy_boxes' );
// Save the Data
function save_${1:custom_meta}(\$post_id) {
global \$${1:custom_meta}_fields;
// verify nonce
if (!wp_verify_nonce(\$_POST['${1:custom_meta}_box_nonce'], basename(__FILE__)))
return \$post_id;
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return \$post_id;
// check permissions
if ('page' == \$_POST['post_type']) {
if (!current_user_can('edit_page', \$post_id))
return \$post_id;
} elseif (!current_user_can('edit_post', \$post_id)) {
return \$post_id;
}
// loop through fields and save the data
foreach (\$${1:custom_meta}_fields as \$field) {
if(\$field['type'] == 'tax_select') continue;
\$old = get_post_meta(\$post_id, \$field['id'], true);
\$new = \$_POST[\$field['id']];
if (\$new && \$new != \$old) {
update_post_meta(\$post_id, \$field['id'], \$new);
} elseif ('' == \$new && \$old) {
delete_post_meta(\$post_id, \$field['id'], \$old);
}
} // enf foreach
// save taxonomies
\$post = get_post(\$post_id);
\$category = \$_POST['category'];
wp_set_object_terms( \$post_id, \$category, 'category' );
}
add_action('save_post', 'save_${1:custom_meta}');
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>metaboxes</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- Optional: Description to show in the menu -->
<description>Create custom meta boxes using the Tammy's Hart system, available at https://github.com/tammyhart/Reusable-Custom-WordPress-Meta-Boxes </description>
</snippet>