Advertisement
Guest User

new image picker

a guest
Aug 19th, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. jQuery(document).ready(function($) {
  2.  
  3. $(document).on('click','.ff-open-image-library-button',function(e){
  4.  
  5. $('.ff-open-image-library-button-opened').removeClass('ff-open-image-library-button-opened');
  6. $(this).parents('.ff-open-image-library-button-wrapper').addClass('ff-open-image-library-button-opened');
  7.  
  8. e.preventDefault();
  9.  
  10. var custom_uploader = wp.media({
  11. title: 'Select Image',
  12. button: { text: 'Select Image' },
  13. library : { type : 'image'},
  14. multiple: false // Set this to true to allow multiple files to be selected
  15. });
  16.  
  17. custom_uploader.on('open',function() {
  18. var selection = custom_uploader.state().get('selection');
  19. var jsoned_value = $('.ff-open-image-library-button-opened').find('input').val();
  20. if( jsoned_value ){ ; } else { return; }
  21.  
  22. try {
  23. obj = JSON.parse( jsoned_value );
  24. } catch(err) {
  25. return;
  26. }
  27.  
  28. if( obj ){ ; } else { return; }
  29. var id = obj.id;
  30.  
  31. var attachment = wp.media.attachment(id);
  32. attachment.fetch();
  33. selection.add( attachment ? [ attachment ] : [] );
  34. });
  35.  
  36. custom_uploader.on('select', function() {
  37. var attachment = custom_uploader.state().get('selection').first().toJSON();
  38. $('.ff-open-image-library-button-opened').find('.custom_media_image').attr('src', attachment.url);
  39. $('.ff-open-image-library-button-opened').find('input').val( attachment.url );
  40. $('.ff-open-image-library-button-opened').find('input').change();
  41. })
  42. .open();
  43. });
  44.  
  45. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement