无法想出一个好的头衔。
我在WordPress中有一个内置版本,我可以使用内置的WordPress媒体上传器上传多个图片。它是如何工作的,在你上传并选择“插入”之后,jQuery将图像路径插入到包含ID的文本字段中。保存后,文本字段将保存在选项表中。
如果您只有1个上传字段,则效果很好。添加更多上传后,每个上传字段都会以相同的图片路径保存。只需要每个上传按钮只插入其相关文本字段的值。
我尝试过使用.each但无法正常使用。还尝试使用.attr('id')来插入值,但没有做任何事情。
这是我的jQuery和标记:
jQuery('.upload-button').click(function() {
formfield = jQuery('.upload').attr('name');
tb_show('', 'media-upload.php?type=image&TB_iframe=true');
return false;
});
window.send_to_editor = function(html) {
imgurl = jQuery('img',html).attr('src');
jQuery('.upload').val(imgurl);
tb_remove();
};
<div class="upload_field"
<input type="text" name="upload_one" id="upload_one" class="upload" value="" />
<input type="button" class="upload-button" value="Upload Image" />
</div>
<div class="upload_field"
<input type="text" name="upload_two" id="upload_two" class="upload" value="" />
<input type="button" class="upload-button" value="Upload Image" />
</div>
<div class="upload_field"
<input type="text" name="upload_three" id="upload_three" class="upload" value="" />
<input type="button" class="upload-button" value="Upload Image" />
</div>
有关详细信息,请参阅我正在使用的上传器设置:http://www.webmaster-source.com/2010/01/08/using-the-wordpress-uploader-in-your-plugin-or-theme/
非常感谢任何帮助。
答案 0 :(得分:27)
只需要能够指定插入值的输入字段。
var uploadID = ''; /*setup the var*/
jQuery('.upload-button').click(function() {
uploadID = jQuery(this).prev('input'); /*grab the specific input*/
formfield = jQuery('.upload').attr('name');
tb_show('', 'media-upload.php?type=image&TB_iframe=true');
return false;
});
window.send_to_editor = function(html) {
imgurl = jQuery('img',html).attr('src');
uploadID.val(imgurl); /*assign the value to the input*/
tb_remove();
};
答案 1 :(得分:6)
我使用它来确保其他功能也适用于编辑器,我还传递了我想要图像的输入字段的id。
<?php
function customPostTypeUploader() {
if(isset($_GET["post_type"])) {
?>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
var uploadID = '';
var storeSendToEditor = '';
var newSendToEditor = '';
jQuery(document).ready(function() {
storeSendToEditor = window.send_to_editor;
newSendToEditor = function(html) {
imgurl = jQuery('img',html).attr('src');
$("#" + uploadID.name).val(imgurl);
tb_remove();
window.send_to_editor = storeSendToEditor;
};
});
function Uploader(id) {
window.send_to_editor = newSendToEditor;
uploadID = id;
formfield = jQuery('.upload').attr('name');
tb_show('', 'media-upload.php?type=image&TB_iframe=true');
return false;
}
</script>
<?php
}
}
add_action("admin_head", "customPostTypeUploader");
?>
然后在元框中使用表单的输入字段,如下所示......
<input type="text" id="image_1" name="uploaded_image_1" value="" size="40" />
<input type="button" onclick="Uploader(image_1);" title="Upload image" class="upload-button" id="add_image" value="Browse..."/>
答案 2 :(得分:4)
这些解决方案的问题在于,您无法将图像插入帖子中,因为这样做的功能已被覆盖。以下是对上述代码的修改,允许图像仍然通过编辑器插入到帖子内容中。
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="150" />')
window.send_to_editor = orig_send_to_editor;
}
return false;
});
});
主要区别在于此代码保存原始函数并将其重新分配给send_to_editor。
这是以它为例的HTML:
<input type="hidden" name="image_1" />
<?php $post_img = get_post_meta($post->ID,'item_image',true); ?>
<input class="upload_image_button" type="button" value="<?php echo (empty($post_img)?'Upload Image':'Change Image') ?>" />
<div id="show_image_1"><?php echo (!empty($post_img)?"<img src='$post_img' width='100' />":'')?></div>
答案 3 :(得分:4)
此示例通过将tb_unload事件绑定为上面提到的soju来恢复window.send_to_editor()函数。
var targetfield = '';
var orig_send_to_editor = window.send_to_editor;
jQuery('.fwb_uploadbtn').click(function() {
targetfield = jQuery(this).prev('.fwb_uploadimg');
tb_show('', 'media-upload.php?type=image&TB_iframe=true');
//restore send_to_editor() when tb closed
jQuery("#TB_window").bind('tb_unload', function () {
window.send_to_editor = orig_send_to_editor;
});
//temporarily redefine send_to_editor()
window.send_to_editor = function(html) {
imgurl = jQuery('img',html).attr('src');
jQuery(targetfield).val(imgurl);
tb_remove();
}
return false;
});
答案 4 :(得分:3)
这是我的演绎:
它还允许您通过检查img src来插入PDF文件或没有SRC属性的任何内容,如果没有,则在文档上尝试href。这个也允许您使用类并将其应用于多个项目。
Javascript:
var formfield = "";
jQuery('.browse_upload').click(function() {
formfield = jQuery(this).attr('name');
tb_show('', 'media-upload.php?type=image&TB_iframe=true');
return false;
});
window.send_to_editor = function(html) {
hrefurl = jQuery('img',html).attr('src');
if(jQuery(hrefurl).length == 0) {
hrefurl = jQuery(html).attr('href'); // We do this to get Links like PDF's
}
jQuery('.' + formfield).val(hrefurl);
tb_remove();
}
示例HTML
<input id="option[unique-option]" class="regular-text unique-option" type="text" name="option[unique-option]" value="<?php esc_attr_e( $options['unique-option'] ); ?>" />
<button class="browse_upload button-secondary" name="unique-option" type="button">Browse</button>
答案 5 :(得分:1)
这肯定是一个难以解决的问题,并没有很好的文档记录,但我认为使用自定义帖子类型和所有... ...我已经改进了保罗Gillespie的代码。
var up = null;
jQuery(document).ready(function() {
up = new Uploader();
});
function Uploader() {
this.storeSendToEditor = window.send_to_editor;
this.uploadID = null;
this.imgID = null;
}
Uploader.prototype.newSendToEditor = function(html) {
imgurl = jQuery('img',html).attr('src');
jQuery("#" + this.up.uploadID).val(imgurl);
if(typeof(this.up.uploadFunc) == "function")
this.up.uploadFunc(html, imgurl, this.up.uploadID);
tb_remove();
this.up.uploadID = '';
window.send_to_editor = this.up.storeSendToEditor;
};
Uploader.prototype.upload = function(id,func){
window.send_to_editor = this.newSendToEditor;
this.uploadID = id;
this.uploadFunc = func;
tb_show('', 'media-upload.php?type=image&TB_iframe=true');
return false;
}
如果您想要做的不仅仅是更新数据库的输入字段,那么通过定义自定义函数来使用它。我想在上传之前在包装div中显示图像。
var uploadFunc = function(html, imgurl, uploadID){
// Do whatever you want.
};
并像以前一样打电话:
<input type="text" id="image_1" name="uploaded_image_1" value="" size="40" />
<input type="button" onclick="up.upload("image_1",uploadFunc);" title="Upload image" class="upload-button" id="add_image" value="Browse..."/>
希望有人发现它有用!
答案 6 :(得分:1)
这可能是一个有点老话题但是按照你的解决方案我的插件工作。上面的代码都没有完全对我的解决方案起作用,但是将它们组合起来我才能使它工作。我需要能够有2个上传字段和一个接受视频文件和其他图像,并且还能够在后期编辑屏幕上发布图像和视频。
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('Add Media', 'media-upload.php?type=file&TB_iframe=true');
window.send_to_editor = function(html) {
imgurl = jQuery('img',html).attr('src');
if(jQuery(imgurl).length == 0) {
imgurl = jQuery(html).attr('href'); // We do this to get Links like PDF's
}
formfield.val(imgurl);
tb_remove();
jQuery('#'+formfield.attr('name')).val(imgurl);
window.send_to_editor = orig_send_to_editor;
}
return false;
});
});
上传字段就是这样
<tr>
<td>
<label for="image_1">Upload Thumbnail</label><br>
<input type="text" name="image_1" id="image_1" value="" size="60" />
<input class="upload_image_button button" type="button" value="Upload Thumbnail" />
<br>
You can also upload thumb from your PC using WordPress media manager(supported files are: .bmp, .BMP, .jpg, .JPG, .png, .PNG, jpeg, JPEG, .gif, .GIF).
</td>
</tr>
<tr>
<td>
<label for="video_1">Upload Video</label><br>
<input type="text" name="video_1" id="video_1" value="" size="60" />
<input class="upload_image_button button" type="button" value="Upload Video" />
<br>
You can also upload video to stream directly from your website, using WordPress media manager(supported files are: .mp4, .MP4, .flv, .FLV, .f4v, .F4V).
</td>
</tr>
这样我就可以上传带有缩略图字段的图像,带有视频字段的视频,也可以在后期编辑屏幕上添加各种图像或图库。
稍后使用php我检查适当的扩展名是否在正确的上传字段中并将其保存在自定义字段中,否则会将字段留空。
也许有些人会觉得这很有用,因为我发现你的答案对我很有帮助。
答案 7 :(得分:1)
这是我的解决方案。这将允许您上传任何文档图像,PDF等&amp;将解决人们将通过文本编辑器获得的冲突问题
var uploadID = ''; // setup the var in a global scope
var original_send_to_editor = window.send_to_editor;
jQuery('.upload-button').click(function() {
uploadID = jQuery(this).prev('input'); // set the uploadID variable to the value of the input before the upload button
formfield = jQuery('.upload').attr('name');
tb_show('', 'media-upload.php?TB_iframe=true');
uploadBAR(); // Call if needed
return false;
});
function uploadBAR() {
window.send_to_editor = function(html) {
imgurl = jQuery(html).attr('href');
uploadID.val(imgurl); /*assign the value of the image src to the input*/
tb_remove();
window.send_to_editor = original_send_to_editor;//restore old send to editor function
};
}
答案 8 :(得分:0)
我用它来上传音频:
jQuery(document).ready(function($) {
$('#upload_button').click(function() {
tb_show('Upload a Podcast MP3 file', 'media-upload.php?referer=my-settings&type=audio&TB_iframe=true&post_id=0', false);
return false;
});
window.send_to_editor = function(html) {
console.log(html);
var file_url = $($.parseHTML(html)).attr('href');
console.log(file_url);
$('#my_input_field').val(file_url);
tb_remove();
}
});