Advertisement
Guest User

WP3.5 Custom Media Upload

a guest
Feb 13th, 2013
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. jQuery(document).ready(function($){
  2.  
  3.   // Uploading files
  4.   var file_frame;
  5.      
  6.   $('.upload_attachments_button').on('click', function( event ){
  7.  
  8.     event.preventDefault();
  9.  
  10.     // If the media frame already exists, reopen it.
  11.     if ( file_frame ) {
  12.       file_frame.open();
  13.       return;
  14.     }
  15.  
  16.     // Create the media frame.
  17.     file_frame = wp.media.frames.file_frame = wp.media({
  18.       title: jQuery( this ).data( 'uploader_title' ),
  19.       button: { text: jQuery( this ).data( 'uploader_button_text' ) },
  20.       library : { type : 'image'},
  21.       multiple: true  // Set to true to allow multiple files to be selected
  22.     });
  23.  
  24.   // When frame is open, select existing image attachments from custom field
  25.   file_frame.on( 'open', function() {
  26.     var selection = file_frame.state().get('selection');
  27.     var attachment_ids = jQuery('#attachment_ids').val().split(',');
  28.     attachment_ids.forEach(function(id) {
  29.       attachment = wp.media.attachment(id);
  30.       attachment.fetch();
  31.       selection.add( attachment ? [ attachment ] : [] );
  32.     });
  33.   });
  34.  
  35.   // When images are selected, place IDs in hidden custom field and show thumbnails.
  36.   file_frame.on( 'select', function() {
  37.  
  38.     var selection = file_frame.state().get('selection');
  39.    
  40.     // Place IDs in custom field
  41.     var attachment_ids = selection.map( function( attachment ) {
  42.       attachment = attachment.toJSON();
  43.       return attachment.id;
  44.     }).join();
  45.     if( attachment_ids.charAt(0) === ',' ) { attachment_ids = attachment_ids.substring(1); }
  46.     $('#attachment_ids').val( attachment_ids );
  47.    
  48.  
  49.     // Show Thumbs
  50.     var attachment_thumbs = selection.map( function( attachment ) {
  51.       attachment = attachment.toJSON();
  52.       console.log('attachment:', attachment);
  53.       return '<img src="' + attachment.sizes.thumbnail.url + '" id="id-' + attachment.id + '" />';
  54.     }).join(' ');
  55.     $('#images-feedback').show();
  56.     $('#thumbs').html(attachment_thumbs);
  57.  
  58.   });
  59.  
  60.     // Finally, open the modal
  61.     file_frame.open();
  62.   });
  63.  
  64.  
  65.   // Place selected thumbnail ID into custom field to save as featured image
  66.   $(document).on('click', '#thumbs img', function() {
  67.      $('#thumbs img').removeClass('chosen');
  68.      var thumb_ID = $(this).attr('id').substring(3);
  69.      $('#wpuf_featured_img').val(thumb_ID);
  70.      $(this).addClass('chosen');
  71.   });
  72.  
  73.   // Show Thumbnails if exist
  74.   if ( $('#attachment_ids').val() ) { $('#images-feedback').show(); }
  75.  
  76.  
  77. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement