Advertisement
kaed

b4b-upload.js - 7/22/13

Jul 22nd, 2013
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 1.24 KB | None | 0 0
  1. jQuery(document).ready(function($){
  2.  
  3.     var custom_uploader;
  4.     var id;
  5.  
  6.     $('.upload').click(function(e) {
  7.  
  8.         e.preventDefault();
  9.        
  10.         //set id of object for comparison
  11.         id = $(this).attr('id');
  12.  
  13.         //If the uploader object has already been created, reopen the dialog
  14.         if (custom_uploader) {
  15.             custom_uploader.open();
  16.             return;
  17.         }
  18.  
  19.         //Extend the wp.media object
  20.         custom_uploader = wp.media.frames.file_frame = wp.media({
  21.             title: 'Choose Image',
  22.             button: {
  23.                 text: 'Choose Image'
  24.             },
  25.             multiple: false
  26.         });
  27.  
  28.         //When a file is selected, grab the URL and set it as the text field's value
  29.         custom_uploader.on('select', function() {
  30.             attachment = custom_uploader.state().get('selection').first().toJSON();
  31.            
  32.             $('.regular-text').each(function(){
  33.                 if ($(this).attr('id') == id){
  34.                     $(this).val(attachment.url);
  35.                 }
  36.             });
  37.            
  38.             $('.preview-upload').each(function(){
  39.                 if($(this).attr('id') == id){
  40.                     $(this).attr('src', attachment.url);
  41.                 }
  42.             });
  43.         });
  44.  
  45.         //Open the uploader dialog
  46.         custom_uploader.open();
  47.  
  48.     });
  49.  
  50. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement