forked from dkrape/WordPress-Custom-Post-Image-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom-post-images.js
61 lines (40 loc) · 1.37 KB
/
custom-post-images.js
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
/*
* Attaches the image uploader to the input field
*/
jQuery(document).ready(function($){
$('#cpi .cpi-upload').each(function() {
var cpi_image_frame;
var p = $(this);
//Choose/upload image
p.find('.cpi-upload-button').click(function(e) {
e.preventDefault();
if ( cpi_image_frame ) {
cpi_image_frame.open();
return;
}
cpi_image_frame = wp.media.frames.cpi_image_frame = wp.media({
title: meta_image.title,
button: { text: meta_image.button }
});
// Runs when an image is selected.
cpi_image_frame.on('select', function() {
// Grabs the attachment selection and creates a JSON representation of the model.
var media_attachment = cpi_image_frame.state().get('selection').first().toJSON();
var media_id = media_attachment.id;
var media_thumbnail = media_attachment.sizes.thumbnail.url;
// Sends the attachment URL to our custom image input field.
p.find('.cpi-upload-id').val(media_id);
p.find('.cpi-upload-thumbnail').html('<img src="' + media_thumbnail + '">');
});
// Opens the media library frame.
cpi_image_frame.open();
});
//Unset current image
p.find('.cpi-upload-clear').click(function(e) {
e.preventDefault();
console.log('clear');
p.find('.cpi-upload-id').val('');
p.find('.cpi-upload-thumbnail').empty();
});
});
});