Advertisement
Guest User

WP Media Uploader JS

a guest
Jul 15th, 2013
614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $button.on('click', function(e){
  2.     // prevent default behavior
  3.     e.preventDefault();
  4.     if ( typeof file_frame != 'undefined' ) {
  5.         file_frame.close();
  6.     }
  7.  
  8.     // create and open new file frame
  9.     file_frame = wp.media({
  10.         //Title of media manager frame
  11.         title: 'Select an Image',
  12.         library: {
  13.             type: 'image'
  14.         },
  15.         button: {
  16.             //Button text
  17.             text: 'Use Image'
  18.         },
  19.         //Do not allow multiple files, if you want multiple, set true
  20.         multiple: false,
  21.     });
  22.  
  23.     //callback for selected image
  24.     file_frame.on('select', function() {
  25.         var selected = [];
  26.         if ( is_multiple ) {
  27.             // multiple images selected
  28.             var selection = file_frame.state().get('selection');
  29.             selection.map(function(file) {
  30.                 selected.push(file.toJSON());
  31.             });
  32.         } else {
  33.             // single image
  34.             selected.push(file_frame.state().get('selection').first().toJSON());
  35.         }
  36.  
  37.         // loop through selected images
  38.         for (var i in selected) {
  39.             console.log(selected[i]);
  40.         }
  41.  
  42.     });
  43.  
  44.     // open file frame
  45.     file_frame.open();
  46. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement