Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- jQuery(document).ready(function($) {
- $(document).on('click','.ff-open-image-library-button',function(e){
- $('.ff-open-image-library-button-opened').removeClass('ff-open-image-library-button-opened');
- $(this).parents('.ff-open-image-library-button-wrapper').addClass('ff-open-image-library-button-opened');
- e.preventDefault();
- var custom_uploader = wp.media({
- title: 'Select Image',
- button: { text: 'Select Image' },
- library : { type : 'image'},
- multiple: false // Set this to true to allow multiple files to be selected
- });
- custom_uploader.on('open',function() {
- var selection = custom_uploader.state().get('selection');
- var jsoned_value = $('.ff-open-image-library-button-opened').find('input').val();
- if( jsoned_value ){ ; } else { return; }
- try {
- obj = JSON.parse( jsoned_value );
- } catch(err) {
- return;
- }
- if( obj ){ ; } else { return; }
- var id = obj.id;
- var attachment = wp.media.attachment(id);
- attachment.fetch();
- selection.add( attachment ? [ attachment ] : [] );
- });
- custom_uploader.on('select', function() {
- var attachment = custom_uploader.state().get('selection').first().toJSON();
- $('.ff-open-image-library-button-opened').find('.custom_media_image').attr('src', attachment.url);
- $('.ff-open-image-library-button-opened').find('input').val( attachment.url );
- $('.ff-open-image-library-button-opened').find('input').change();
- })
- .open();
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement