自动将 Wordpress 图像包装到 [caption] 短代码中
Automaticalls wrap Wordpress image into [caption] shortcode
我正在开发自己的 wordpress 主题。
然而,一位编辑抱怨他输入的图片标题不会在网站上显示。经过一些研究,我发现当您将图像插入 post.
时,Wordpress 不会自动添加上传图像的标题。
Example: I click on "add media" in my post and upload the image
peter.jpg. After that I enter "My caption" in the captions field and
click on the insert button. Now the picture appears in my WYSIWIG
editor from wordpress but the caption is missing. When I now edit the
image I see that the caption field remaind empty. If I enter a caption
now it wraps the whole image into a [caption] shortcode and everything
is fine.
嗯,这种有趣的行为在 Worpress 中很正常(据我在 google 的一些文章中发现)。如果您将图像插入 post.
的编辑器字段,则为任何图像输入的标题不会自动出现
所以我不明白为什么这是有道理的,但我的编辑抱怨 "No sir, I do not like typing the caption twice …" 所以我试图自动接管
插入的标题
add_filter('image_send_to_editor', 'automatically_wrap_my_post_images', 11, 8);
function automatically_wrap_my_post_images($html, $id, $caption, $title, $align, $url, $size, $alt){
return '[caption id="attachment_'.$id.'" align="align'.$align.'" width="620"]'.$html.$caption.'[/caption]';
}//wrap_my_div
并将整个图像包裹在 [caption] 简码中。
但没有任何效果,在我点击保存一次后删除了短代码,只剩下文本。
那么有什么简单的方法可以自动将编辑器中的任何 post 图像包装成常规 [caption] 简码和原始图像标题。这样您就不需要在将图像插入 post 编辑器后再次输入标题了?
谢谢
抱歉,这不能直接回答您关于字幕的问题(应该有一个插件,但如果没有,您的代码看起来应该可以工作)。
这 "funny behaviour" 在 WordPress 中不正常,只是因为
a) php 内存问题(内存不足,csf 防火墙终止进程占用太多 long/use 太多内存)
b) mysql 由于硬盘填满而无法正常执行 and/or 没有 mysql 数据库驻留在其自己的分区中以获得最佳性能
c) 服务器 tmpdisk 已满,因为会话缓存由于插件或主题的草率代码而未被清除,或者仅仅因为它小得离谱,如 500MB 之类。
如果 WordPress 所在的服务器没有任何这些问题,它就会运行得很好。
我正在开发自己的 wordpress 主题。 然而,一位编辑抱怨他输入的图片标题不会在网站上显示。经过一些研究,我发现当您将图像插入 post.
时,Wordpress 不会自动添加上传图像的标题。Example: I click on "add media" in my post and upload the image peter.jpg. After that I enter "My caption" in the captions field and click on the insert button. Now the picture appears in my WYSIWIG editor from wordpress but the caption is missing. When I now edit the image I see that the caption field remaind empty. If I enter a caption now it wraps the whole image into a [caption] shortcode and everything is fine.
嗯,这种有趣的行为在 Worpress 中很正常(据我在 google 的一些文章中发现)。如果您将图像插入 post.
的编辑器字段,则为任何图像输入的标题不会自动出现所以我不明白为什么这是有道理的,但我的编辑抱怨 "No sir, I do not like typing the caption twice …" 所以我试图自动接管
插入的标题 add_filter('image_send_to_editor', 'automatically_wrap_my_post_images', 11, 8);
function automatically_wrap_my_post_images($html, $id, $caption, $title, $align, $url, $size, $alt){
return '[caption id="attachment_'.$id.'" align="align'.$align.'" width="620"]'.$html.$caption.'[/caption]';
}//wrap_my_div
并将整个图像包裹在 [caption] 简码中。
但没有任何效果,在我点击保存一次后删除了短代码,只剩下文本。
那么有什么简单的方法可以自动将编辑器中的任何 post 图像包装成常规 [caption] 简码和原始图像标题。这样您就不需要在将图像插入 post 编辑器后再次输入标题了?
谢谢
抱歉,这不能直接回答您关于字幕的问题(应该有一个插件,但如果没有,您的代码看起来应该可以工作)。 这 "funny behaviour" 在 WordPress 中不正常,只是因为
a) php 内存问题(内存不足,csf 防火墙终止进程占用太多 long/use 太多内存)
b) mysql 由于硬盘填满而无法正常执行 and/or 没有 mysql 数据库驻留在其自己的分区中以获得最佳性能
c) 服务器 tmpdisk 已满,因为会话缓存由于插件或主题的草率代码而未被清除,或者仅仅因为它小得离谱,如 500MB 之类。
如果 WordPress 所在的服务器没有任何这些问题,它就会运行得很好。