Wordpress自定义图片上传字段
问题描述:
我目前正在开发一个需要自定义内容添加/编辑模块的WordPress网站。我正在寻找wordpress中的图片上传选项,例如。在我的表单中有一个标题为“选择图像”的输入字段,当用户点击输入字段时,我想让wordpress媒体库弹出并能够从图库中选择图像。并且一旦用户选择了图像,图像的完整url将被填充到输入字段中。我确信这可以做到,因为我曾经为此找到过一个代码,但是我忘记了我发现它的地方。请帮助大家Wordpress自定义图片上传字段
在此先感谢..
答
这里是代码,将做到这一点:
jQuery(document).ready(function() {
var orig_send_to_editor = window.send_to_editor;
jQuery('#upload_image_button').click(function() {
formfield = jQuery(this).prev('input');
tb_show('', 'media-upload.php?type=image&TB_iframe=true');
window.send_to_editor = function(html) {
var regex = /src="(.+?)"/;
var rslt =html.match(regex);
var imgurl = rslt[1];
formfield.val(imgurl);
tb_remove();
jQuery('#show_'+formfield.attr('name')).html('<img src="'+imgurl+'" width="" />')
window.send_to_editor = orig_send_to_editor;
}
return false;
});
});
下面是HTML去用它:
<input type="hidden" name="upload_image" />
<?php $post_img = get_post_meta($post->ID,'item_image',true); ?>
<input id="upload_image_button" type="button" value="<?php echo (empty($post_img)?'Upload Image':'Change Image') ?>" />
<div id="show_upload_image"><?php echo (!empty($post_img)?"<img src='$post_img' width='100' />":'')?></div>
请访问http:/ /wordpress.stackexchange.com/questions/4307/how-can-i-add-an-image-upload-field-directly-to-a-custom-write-panel – rhand