-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwp.image_upload.php
130 lines (106 loc) · 3.18 KB
/
wp.image_upload.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
<?php
add_action('admin_menu', 'add_meta_boxes');
function add_meta_boxes(){
add_meta_box( 'meta_id', 'META TITLE', 'display_meta', 'page', 'advanced', 'high', array('test') );
}
function display_meta($post, $meta){
$images = get_post_meta($post->ID, 'image_list', true);
echo '<ul class="image_list">';
if($images)
foreach($images as $url){
echo "<li style='background-image:url({$url})'><input name='image_url[]' class='image_url' type='text' value='{$url}'/>";
}
echo "<li><input class='image_upload button-primary' type='submit' value='Upload'/><input name='image_url[]' class='image_url' type='text'/>";
echo '</ul><br style="clear:both">';
}
add_action('save_post', 'meta_box_save');
function meta_box_save($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;
}
update_post_meta($post_id, 'image_list', $_POST['image_url']);
}
add_action('admin_head', 'ui_scripts');
function ui_scripts(){
?>
<script>
(function($){
$(function(){
var $image_lists = $('ul.image_list'),
idx = $image_lists.length,
image_template = "<li style='background-image:url({$url})'><input name='image_url[]' class='image_url' type='text' value='{$url}' style='display:none'/><div class='delete_image'>X</div";
$image_lists.sortable();
while(idx){
$list = $image_lists.eq(--idx);
$list.find('li').not(':last').append('<div class="delete_image">X</div>');
$list.find('input.image_url').hide().last().remove();
$list.delegate('div.delete_image', 'click', function(e){
$(this).parent().remove();
});
$list.delegate('input.image_upload', 'click', function(e){
tb_show('', 'media-upload.php?TB_iframe=true');
window.send_to_editor = function(html){
var url = $(html).attr('href'),
new_image = image_template.replace(/\{\$url\}/g, url);
$list.prepend(new_image);
tb_remove();
};
e.preventDefault();
});
}
});
})(jQuery);
</script>
<style>
.image_list li{
position: relative;
background: #e2e2e2;
border: 5px solid #e2e2e2;
height: 100px;
width: 100px;
text-align: center;
float: left;
margin: 0 10px 10px 0;
background-size: contain;
background-position: center;
background-repeat: no-repeat;
}
.image_list li:hover .delete_image{
display: block;
}
.image_upload{
margin-top: 34px;
}
.image_url{
position: absolute;
width: 100%;
margin: 0;
bottom: -1px;
left: 0;
}
.delete_image{
display: none;
position: absolute;
top: -9px;
right: -9px;
background: #777;
border-radius: 50%;
width: 17px;
height: 14px;
font: bold 11px Arial;
color: white;
padding-top: 2px;
border: 2px solid #888;
}
</style>
<?php
}