Php Wordpress自定义图像上载字段
我目前正在开发一个WordPress网站,它需要一个自定义内容添加/编辑模块。我正在wordpress中寻找图像上传选项,例如,在我的表单中有一个名为“选择图像”的输入字段,当用户单击输入字段时,我希望wordpress媒体库弹出并能够从库中选择图像。一旦用户选择了一个图像,图像的完整url将填充到输入字段中。我相信这是可以做到的,因为我曾经找到了一个代码,但我忘了在哪里找到它。请帮助大家Php Wordpress自定义图像上载字段,php,image,wordpress,upload,Php,Image,Wordpress,Upload,我目前正在开发一个WordPress网站,它需要一个自定义内容添加/编辑模块。我正在wordpress中寻找图像上传选项,例如,在我的表单中有一个名为“选择图像”的输入字段,当用户单击输入字段时,我希望wordpress媒体库弹出并能够从库中选择图像。一旦用户选择了一个图像,图像的完整url将填充到输入字段中。我相信这是可以做到的,因为我曾经找到了一个代码,但我忘了在哪里找到它。请帮助大家 提前感谢。以下是执行此操作的代码: jQuery(document).ready(function() {
提前感谢。以下是执行此操作的代码:
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;
});
});
jQuery(文档).ready(函数(){
var orig_send_to_editor=window.send_to_editor;
jQuery(“#上传_图像_按钮”)。单击(函数(){
formfield=jQuery(this.prev('input');
tb_show('''media upload.php?type=image&tb_iframe=true');
window.send_to_editor=函数(html){
var regex=/src=“(.+?)”/;
var rslt=html.match(regex);
var imgurl=rslt[1];
formfield.val(imgurl);
tb_remove();
jQuery('#show'+formfield.attr('name')).html('')
window.send_to_editor=orig_send_to_editor;
}
返回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>
看见