Jquery window.send_to_editor
无法为此想出一个好的标题。
我在 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 /
一如既往,我们非常感谢任何帮助。
Couldn't come up with a good title for this.
I've got a build in WordPress where I have multiple image uploads using the built in WordPress media uploader. How it's working is, after you upload and choose "insert", jQuery is inserting the image path into a text field, which contains the ID. Once you save, the text field is saved in the options table.
Works perfectly if you only have 1 upload field. Once you add more uploads, every upload field gets saved with the same image path. Just need each upload button to only insert the value for it's associated text field.
I've tried to use .each but couldn't get it working correctly. Also tried using .attr('id') to the value insert, but nothing doing there.
Here is my jQuery and the markup:
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>
For further info, here is the uploader setup I'm using: http://www.webmaster-source.com/2010/01/08/using-the-wordpress-uploader-in-your-plugin-or-theme/
Any help, as always, is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
只需能够指定插入值的输入字段。
Just need to be able to specify the input field that the value is inserted into.
我用它来确保其他功能也适用于编辑器,并且我还传递了我想要图像的输入字段的 ID。
然后在元框中使用表单的输入字段,如下所示...
I use this to ensure other functionality works too for the editor and I also pass the id of the input field that I want the image in.
Then use the input fields of a form in your meta box like this...
这些解决方案的问题在于,您将无法将图像插入帖子中,因为执行此操作的功能已被覆盖。这是对上述代码的修改,允许图像仍然通过编辑器插入到帖子内容中。
主要区别在于,此代码保存了原始函数并将其重新分配回 send_to_editor。
以下是与之配套的 HTML 作为示例:
The problem with these solutions is that you won't be able to insert images into posts as the function to do so has been overridden. Here is a modification to the above code that will allow images to still be inserted into the post content through the editor.
The main difference is that this code saves the original function and reassigns it back to send_to_editor.
Here is the HTML to go with it as an example:
此示例通过绑定 tb_unload 事件(如上所述 soju)来恢复 window.send_to_editor() 函数。
This example restores the window.send_to_editor() func by binding the tb_unload event as soju mentioned above.
这是我的演绎:
它还允许您通过检查 img src 来插入 PDF 文件或任何没有 SRC 属性的内容,如果没有,则尝试在文档上使用 href。这还允许您使用类并将其应用于多个项目。
Javascript:
示例 HTML
Here is my rendition:
It also allows you to insert PDF's files or anything without a SRC attribute by doing a check for img src and if none, trying href on the document. This one also allows you to use classes and apply this to multiple items.
The Javascript:
Sample HTML
这确实是一个很难解决的问题,没有很好的记录,但我认为自定义帖子类型和所有内容的使用正在增加……恕我直言,我对 Paul Gillespie 的代码进行了一些改进。
如果您想做的不仅仅是更新数据库的输入字段,那么可以通过定义自定义函数来使用它。我想在上传之前在包装 div 中显示图像。
几乎像以前一样称呼它:
希望有人发现它有用!
It sure is a hard issue to solve, not very well documented, but I think use is increasing with custom post types and all... I've imho improved somewhat on Paul Gillespie's code.
Then use it by defining the custom function if you want to do more than just updating the input field for the database. I for one want to display the images within a wrapper div before uploading it.
And call it almost as before:
Hope someone finds it useful!
这可能是一个有点老的话题,但按照您的解决方案,我使我的插件正常工作。上面的代码都没有完全适合我的解决方案,但将它们结合起来我使它工作。我需要能够有 2 个上传字段,一个用于接受视频文件和其他图像,并且还能够在后期编辑屏幕中发布图像和视频。
上传字段是这样的
这样我可以上传带有缩略图字段的图像,以及带有视频字段的视频,并在帖子编辑屏幕中添加各种图像或图库。
稍后,我使用 php 检查正确的扩展名是否在正确的上传字段中,并将它们保存在自定义字段中,如果没有,则将字段留空。
也许有些人会发现这很有用,因为我发现您的答案对我有用且有帮助。
This is maybe a little bit old topic but following your solutions i made my plugin work. None of the code above didn't worked completely to my solution, but combining them i made it work. I needed to be able to have 2 upload fields and one to accept video files and other images, and to be able also to post images and videos in post edit screen.
And fields for upload are like this
This way i can upload image with thumbnail field, and video with video field, and add various images or gallery in post edit screen also.
Later with php i check if proper extensions are in proper upload fields and save them in custom fields, if not it leave the fields empty.
Maybe some will find this useful as i found your answers useful and helpful to me.
这是我的解决方案。这将允许您上传任何文档图像、PDF 等。将解决文本编辑器遇到的冲突问题
Here is my solution. This will allow you to upload any doc images, PDF's etc & will sort out the conflict issues that one will get with the text editors
我用它来上传音频:
I used this for uploading audio: