【问题标题】:WordPress: image_send_to_editor retrieve selected image width and heightWordPress:image_send_to_editor 检索选定的图像宽度和高度
【发布时间】:2013-08-09 03:22:18
【问题描述】:

我修改了 functions.php 中的“image_send_to_editor”代码,用于编辑器添加的图像标题和图像。特别是从媒体库中选择的图像。

目前,我无法访问所选图像的宽度和高度。缺少什么变量使这成为可能?理想情况下,图像标签应如下所示。

<img src='$url' alt='$title' class='size-$size' width='IMAGE-WIDTH' height='IMAGE-HEIGHT'>

// image and caption replace 
function html5_insert_image($html, $id, $caption, $title, $align, $url, $size) {
    $html5 = "<figure id='post-$id media-$id' class='figure align$align'>";
    $html5 .= "<img src='$url' alt='$title' class='size-$size'>";
    if ($caption) {
        $html5 .= "<figcaption>$caption</figcaption>";
    }
    $html5 .= "</figure>";
    return $html5;
}
add_filter( 'image_send_to_editor', 'html5_insert_image', 10, 9 );

感谢任何帮助。谢谢!

【问题讨论】:

  • 我已经在一个项目中尝试了这个和来自网络的类似 sn-ps,到目前为止我注意到有两件事可能会导致问题。首先,如果您尝试编辑现有图像的标题,则会导致混乱。其次,删除图像只会删除 img 标签。它将图形标签留在编辑器中,对使用非 HTML 的用户不可见。如果有人有解决这些问题的方法,我很想听听,因为 WP 不是我的正常驾驶室。
  • @Charasan 你试过下面的代码了吗?你能解决你的问题吗?
  • 我有一段时间没有重温这个了。更改
    标签会解决我遇到的问题吗?我将尝试更改,看看效果如何。
  • 抱歉,它看起来确实更干净。至少是标题部分,但它忽略了 figcaption 以支持默认的 WP 行为,并且在通过 WP 工具删除图像时
    标签仍然保留。抱歉,我有段时间没有重温这个问题,忘记在这里了。
  • 我不确定,我了解您的问题。你有例子吗?

标签: image wordpress replace editor


【解决方案1】:

我设法使用“get_image_tag”功能解决了这个问题。这将返回图像的宽度和高度。现在我可以从媒体库中挑选不同尺寸的图片了。

function html5_insert_image($html, $id, $caption, $title, $align, $url, $size) {

    /**** START - ADD THIS LINE TO RETRIEVE THE WIDTH & HEIGHT ****/
    $html = get_image_tag($id, '', $title, $align, $size);    
    /**** END ****/

    $html5 = "<figure id='post-$id media-$id' class='figure align$align'>";
    $html5 .= $html;
    if ($caption) {
        $html5 .= "<figcaption>$caption</figcaption>";
    }
    $html5 .= "</figure>";
    return $html5;
}
add_filter( 'image_send_to_editor', 'html5_insert_image', 10, 9 );

【讨论】:

  • 我测试了这段代码没有任何问题,但它不适用于这些带有标题的图像
猜你喜欢
  • 2011-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多