wp_media_uploader.js
3.3 KB
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
jQuery(function($){
/*
* Select/Upload image(s) event
*/
$('body').on('click', '.misha_upload_image_button', function(e){
e.preventDefault();
var button = $(this),
custom_uploader = wp.media({
title: 'Insert image',
library : {
// uncomment the next line if you want to attach image to the current post
// uploadedTo : wp.media.view.settings.post.id,
type : 'image'
},
button: {
text: 'Use this image' // button label text
},
multiple: false // for multiple image selection set to true
}).on('select', function() { // it also has "open" and "close" events
var attachment = custom_uploader.state().get('selection').first().toJSON();
$(button).removeClass('button').html('<img class="true_pre_image" src="' + attachment.url + '" style="max-width:100px;height:100px;display:block;" />').next().val(attachment.id).next().show();
jQuery(".hidden_img").val(attachment.url);
/* if you sen multiple to true, here is some code for getting the image IDs
var attachments = frame.state().get('selection'),
attachment_ids = new Array(),
i = 0;
attachments.each(function(attachment) {
attachment_ids[i] = attachment['id'];
console.log( attachment );
i++;
});
*/
})
.open();
});
$('body').on('click', '.misha_upload_image_button_failer', function(e){
e.preventDefault();
var button = $(this),
custom_uploader = wp.media({
title: 'Insert image',
library : {
// uncomment the next line if you want to attach image to the current post
// uploadedTo : wp.media.view.settings.post.id,
type : 'image'
},
button: {
text: 'Use this image' // button label text
},
multiple: false // for multiple image selection set to true
}).on('select', function() { // it also has "open" and "close" events
var attachment = custom_uploader.state().get('selection').first().toJSON();
$(button).removeClass('button').html('<img class="true_pre_image" src="' + attachment.url + '" style="max-width:100px;height:100px;display:block;" />').next().val(attachment.id).next().show();
jQuery(".failure_hidden_img").val(attachment.url);
/* if you sen multiple to true, here is some code for getting the image IDs
var attachments = frame.state().get('selection'),
attachment_ids = new Array(),
i = 0;
attachments.each(function(attachment) {
attachment_ids[i] = attachment['id'];
console.log( attachment );
i++;
});
*/
})
.open();
});
/*
* Remove image event
*/
$('body').on('click', '.misha_remove_image_button', function(){
$(this).hide().prev().val('').prev().addClass('button').html('Upload image');
return false;
});
});