Skip to content

Instantly share code, notes, and snippets.

@htvu
Created April 10, 2013 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save htvu/5355294 to your computer and use it in GitHub Desktop.
Save htvu/5355294 to your computer and use it in GitHub Desktop.
New Media Upload for OptionTree
// Check for WP 3.5. wp object only available in WP 3.5
if (typeof wp !== 'undefined') {
OT_UI.init_upload = function() {
var _custom_media = true,
_orig_send_attachment = wp.media.editor.send.attachment;
$('.ot_upload_media').live('click', function(event) {
var field_id = $(this).parent('.option-tree-ui-upload-parent').find('input').attr('id'),
post_id = $(this).attr('rel'),
btnContent = '';
var send_attachment_bkp = wp.media.editor.send.attachment;
_custom_media = true;
wp.media.editor.send.attachment = function(props, attachment){
if ( _custom_media ) {
var href = attachment.url;
var image = /\.(?:jpe?g|png|gif|ico)$/i;
if (OT_UI.url_exists(href)) {
if (href.match(image)) {
btnContent += '<div class="option-tree-ui-image-wrap"><img src="'+href+'" alt="" /></div>';
}
btnContent += '<a href="javascript:(void);" class="option-tree-ui-remove-media option-tree-ui-button" title="'+option_tree.remove_media_text+'"><span class="icon trash-can">'+option_tree.remove_media_text+'</span></a>';
}
$('#'+field_id).val(href);
$('#'+field_id+'_media').remove();
$('#'+field_id).parent().parent('div').append('<div class="option-tree-ui-media-wrap" id="'+field_id+'_media" />');
$('#'+field_id+'_media').append(btnContent).slideDown();
OT_UI.fix_upload_parent();
} else {
return _orig_send_attachment.apply( this, [props, attachment] );
};
}
wp.media.editor.open($(this));
return false;
});
$('a.add_media').on('click', function() {
_custom_media = false;
});
};
} // END check for WP 3.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment