WordPress 3.5 is just released with a number of major changes. The one I love most is HiDPI display support, WordPress looks so beautiful on my MacBook Pro Retina display. Besides, after the update was released I considered upgrading my StagFramework to the WordPress 3.5 standard, going ahead I implemented media uploader and color picker along with a couple of new functions in WP 3.5.
So as you all know that WordPress has got a new media uploader so why not to use it our theme options, WordPress theme developer would be happy working with this because it has got less hassle than previous media uploader; you don’t need to do any enqueue any script like we did before wp_enqueue_script('media-upload');. So let’s go ahead and see how can we achieve this.
I assume you have following code for uploader, that’s what I have used.
<div class="uploader">
<input type="text" name="settings[_unique_name]" id="_unique_name" />
<input class="button" name="_unique_name_button" id="_unique_name_button" value="Upload" />
</div>
Once we have code above now let’s write some jQuery for upload button to work.
jQuery(document).ready(function($){
var _custom_media = true,
_orig_send_attachment = wp.media.editor.send.attachment;
$('.stag-metabox-table .button').click(function(e) {
var send_attachment_bkp = wp.media.editor.send.attachment;
var button = $(this);
var id = button.attr('id').replace('_button', '');
_custom_media = true;
wp.media.editor.send.attachment = function(props, attachment){
if ( _custom_media ) {
$("#"+id).val(attachment.url);
} else {
return _orig_send_attachment.apply( this, [props, attachment] );
};
}
wp.media.editor.open(button);
return false;
});
$('.add_media').on('click', function(){
_custom_media = false;
});
});
Bingo! We are done here. As you click on upload button you will see the new media uploader. If you want to play around the attachment, you can just do a console.log(attachment) or alert(attachment).
Meanwhile, if you come up with any better solution let me know in the comments.
wp_enqueue_media(); in your theme. Thanks to daviedR for correcting me on this.
Pingback: 2012 Year in Review - Ram Ratan Maurya
Pingback: smartcode | blog | WordPress 3.5 Media Uploader in Meta Box Plugin