uploaders.js
1.67 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
(function($) {
$(document).ready(function() {
var frame;
$( 'a.preview-link' ).on( 'click', function( e ) {
if ( $( this ).hasClass( 'remove' ) ) {
var placeholder_image = $( this ).parents( '.form-table' ).parents( '.form-table' ).find( '.subscribe-and-connect-placeholder-image' ).attr( 'src' );
$( this ).find( 'img' ).attr( 'src', placeholder_image );
$( this ).addClass( 'add' ).removeClass( 'remove' );
return false;
} else {
var $el = $( this );
event.preventDefault();
file_path_field = $el.parents( '.subscribe-and-connect-network-item' ).find( '.upload-id' );
// If the media frame already exists, reopen it.
if ( frame ) {
frame.open();
return;
}
frame = wp.media({
title: $el.data( 'uploader-title' ),
button: {
text: $el.data( 'uploader-button-text' ),
},
multiple: false, // Set to true to allow multiple files to be selected
library: {
type: 'image'
}
});
// When an image is selected, run a callback.
frame.on( 'select', function() {
// We set multiple to false so only get one image from the uploader
var attachment = frame.state().get('selection').first().toJSON();
// Do something with attachment.id and/or attachment.url here
$( file_path_field ).val( attachment.id );
// Small preview of the image
$( file_path_field ).parents( '.subscribe-and-connect-network-item' ).find( '.image-preview img' ).attr( 'src', attachment.url );
// Swap out the CSS classes
$( this ).addClass( 'remove' ).removeClass( 'add' );
});
// Finally, open the modal
frame.open();
}
});
});
})(jQuery);