Advertisement
keihead

js for image uploader

Jul 13th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. jQuery(document).ready(function($){
  2.     var custom_uploader;
  3.     $('#upload_image_button').click(function(e) {
  4.         e.preventDefault();
  5.         //If the uploader object has already been created, reopen the dialog
  6.         if (custom_uploader) {
  7.             custom_uploader.open();
  8.             return;
  9.         }
  10.  
  11.         //Extend the wp.media object
  12.         custom_uploader = wp.media.frames.file_frame = wp.media({
  13.             title: 'Choose Image',
  14.             button: {
  15.                 text: 'Choose Image'
  16.             },
  17.             multiple: false
  18.         });
  19.  
  20.         //When a file is selected, grab the URL and set it as the text field's value
  21.         custom_uploader.on('select', function() {
  22.             attachment = custom_uploader.state().get('selection').first().toJSON();
  23.             $('#upload_image').val(attachment.url);
  24.         });
  25.  
  26.         //Open the uploader dialog
  27.         custom_uploader.open();
  28.  
  29.     });
  30.         jQuery('.custom_clear_image_button').click(function() {  
  31.         var defaultImage = jQuery(this).parent().siblings('.custom_default_image').text();  
  32.         jQuery(this).parent().siblings('.custom_upload_image').val('');  
  33.         jQuery(this).parent().siblings('.custom_preview_image').attr('src', defaultImage);  
  34.         return false;  
  35.     });  
  36.  
  37. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement