WordPress 3.5 自定义媒体上传为您的主题选项 [英] WordPress 3.5 custom media upload for your theme options
问题描述
WordPress 3.5 最近发布了,我通过thickbox 和window.send_to_editor
使用了WordPress 媒体上传系统作为我的WordPress 主题中的一些选项(背景、徽标等...).
WordPress 3.5 has been released recently, I used the WordPress Media Upload system via thickbox and window.send_to_editor
for some options in my WordPress theme (backgrounds, logos etc...).
但正如您所知,WordPress 集成了一个新的媒体管理器,我想使用这个新功能将图像/文件作为自定义字段上传.于是我花了一上午时间想办法得到想要的结果.
But as you know WordPress has integrated a new Media Manager, I wanted to used this new feature to upload images/files as custom fields. So I spent the morning to find a way to get the wished result.
我找到了这个解决方案,它对你们中的一些人很有用.感谢向我提供您对代码的反馈或您想到的任何改进!
I found with this solution, which can be useful for some of you. Thanks to give me your feedback on the code or any improvements you have in mind!
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)
.
If you want to see every settings contained in the attachment
variable you can do a console.log(attachment)
or alert(attachment)
.
推荐答案
如果您不在帖子编辑页面,请不要忘记使用 wp_enqueue_media
(3.5 中的新功能)
Don't forget to use wp_enqueue_media
(new in 3.5) if you'r not on the post edit page
要使用旧的媒体上传框,您可以这样做:
To use the old media upload box you can do this:
if(function_exists( 'wp_enqueue_media' )){
wp_enqueue_media();
}else{
wp_enqueue_style('thickbox');
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
}
这篇关于WordPress 3.5 自定义媒体上传为您的主题选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!