WordPress 3.5已于近期发布,我通过thickbox使用了WordPress Media Upload系统,并window.send_to_editor
在WordPress主题中使用了某些选项(背景,徽标等...)。
但是,您知道WordPress集成了新的媒体管理器,所以我想使用此新功能将图像/文件上传为自定义字段。因此,我花了整个上午寻找找到理想结果的方法。
我发现此解决方案对您中的某些人可能有用。感谢您给我有关代码或您所想到的任何改进的反馈!
HTML示例:
<a href="#" class="custom_media_upload">Upload</a>
<img class="custom_media_image" src="" />
<input class="custom_media_url" type="text" name="attachment_url" value="">
<input class="custom_media_id" type="text" name="attachment_id" value="">
jQuery代码:
$('.custom_media_upload').click(function() {
var send_attachment_bkp = wp.media.editor.send.attachment;
wp.media.editor.send.attachment = function(props, attachment) {
$('.custom_media_image').attr('src', attachment.url);
$('.custom_media_url').val(attachment.url);
$('.custom_media_id').val(attachment.id);
wp.media.editor.send.attachment = send_attachment_bkp;
}
wp.media.editor.open();
return false;
});
如果要查看attachment
变量中包含的所有设置,可以执行console.log(attachment)
或alert(attachment)
。