Skip to content

Instantly share code, notes, and snippets.

@jeffsebring
Created September 16, 2013 19:31
Show Gist options
  • Save jeffsebring/6585368 to your computer and use it in GitHub Desktop.
Save jeffsebring/6585368 to your computer and use it in GitHub Desktop.
WP 3.5 Options Page Modal Uploader
jQuery( document ).ready( function() {
function pu01_show_image() {
var pu01_image_1 = jQuery( '#pu01_image_1' ).val(),
pu01_image_2 = jQuery( '#pu01_image_2' ).val(),
pu01_image_3 = jQuery( '#pu01_image_3' ).val();
jQuery( '#pu01_image_1_preview' ).after( '<div id="pu01_image_1_preview_image"><img width="300" height="140" src="' + pu01_image_1 + '" /></div>' );
jQuery( '#pu01_image_2_preview' ).after( '<div id="pu01_image_2_preview_image"><img width="300" height="140" src="' + pu01_image_2 + '" /></div>' );
jQuery( '#pu01_image_3_preview' ).after( '<div id="pu01_image_3_preview_image"><img width="300" height="140" src="' + pu01_image_3 + '" /></div>' );
}
pu01_show_image();
var file_frame;
var wp_media_post_id = wp.media.model.settings.post.id;
jQuery( '.upload_image_button' ).on( 'click', function( event ){
event.preventDefault();
var image_input = jQuery( this ).data( 'input' );
if ( file_frame ) {
file_frame.uploader.uploader.param( 'post_id', wp_media_post_id )
file_frame.open();
return;
} else {
wp.media.model.settings.post.id = wp_media_post_id;
}
file_frame = wp.media.frames.file_frame = wp.media({
title: jQuery( this ).data( 'uploader_title' ),
button: {
text: jQuery( this ).data( 'uploader_button_text' ),
},
multiple: false
});
file_frame.on( 'select', function() {
attachment = file_frame.state().get('selection').first().toJSON();
jQuery( image_input ).val( attachment.url );
jQuery( image_input + '_preview_image' ).empty();
jQuery( image_input + '_preview_image' ).html( '<img width="300" height="140" src="' + attachment.url + '" />' );
wp.media.model.settings.post.id = wp_media_post_id;
});
file_frame.open();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment