Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $button.on('click', function(e){
- // prevent default behavior
- e.preventDefault();
- if ( typeof file_frame != 'undefined' ) {
- file_frame.close();
- }
- // create and open new file frame
- file_frame = wp.media({
- //Title of media manager frame
- title: 'Select an Image',
- library: {
- type: 'image'
- },
- button: {
- //Button text
- text: 'Use Image'
- },
- //Do not allow multiple files, if you want multiple, set true
- multiple: false,
- });
- //callback for selected image
- file_frame.on('select', function() {
- var selected = [];
- if ( is_multiple ) {
- // multiple images selected
- var selection = file_frame.state().get('selection');
- selection.map(function(file) {
- selected.push(file.toJSON());
- });
- } else {
- // single image
- selected.push(file_frame.state().get('selection').first().toJSON());
- }
- // loop through selected images
- for (var i in selected) {
- console.log(selected[i]);
- }
- });
- // open file frame
- file_frame.open();
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement