从文章中How to use WordPress 3.5 Media Uploader in Theme Options (由codestag.com提供),如中所示Using the WordPress 3.5 Media Uploader within plugins (由mikejolley.com提供)<迈克·乔利的文章有一些很好的技巧
Important note: 如果要使用上载程序的页面doesn\'t already have 所有媒体JS API,然后wp_enqueue_media()
必须打电话。
因此,假设我们在自定义元框中有这样一个按钮:
<div class="uploader">
<input type="text" name="settings[_wpse_82857]" id="_wpse_82857" />
<input class="button" name="_wpse_82857_button" id="_wpse_82857_button" value="Upload" />
</div>
下面的脚本将调用新的上载程序并填充输入文本字段
#_wpse_82857
单击“插入帖子”时的附件路径。
jQuery(document).ready(function($)
{
var _custom_media = true,
_orig_send_attachment = wp.media.editor.send.attachment;
// ADJUST THIS to match the correct button
$(\'.uploader .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;
});
});