我目前正在开发一个 WordPress 网站,需要自定义内容添加/编辑模块。我正在寻找 WordPress 中的图像上传选项,例如。在我的表单中有一个名为“选择图像”的输入字段,当用户单击输入字段时,我希望弹出 WordPress 媒体库并能够从库中选择图像。一旦用户选择了图像,图像的完整 URL 将被填充到输入字段中。我确信这是可以做到的,因为我曾经找到过这样的代码,但我忘记了在哪里找到的。请大家帮忙
提前致谢..
最佳答案
下面是执行此操作的代码:
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>
关于php - WordPress 自定义图片上传字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8475531/