0

I'm using WP 3.6.1 and when I insert an image in post add\edit screen it generates the text like

<a href="...">
<img class="alignnone size-medium wp-image-8858" alt="..." src="..." width="300" height="168"/>
</a>

But I'd like to have the caption shortcode generated automatically as well. Here is an example:

[caption id="attachment_8858" align="alignnone" width="300"]
<a href="...">
<img class="alignnone size-medium wp-image-8858" alt="..." src="..." width="300" height="168"/>
</a>Caption text
[/caption]

How could it be accomplished? I know that some themes\plugins allow for that, but I cannot find such.

Thanks in advance

4

1 回答 1

0

您使用过滤器挂钩image_send_to_editor。它允许操作正在插入的 HTML。

<?php
/**
 * Plugin Name: Add Caption to Inserted Images
 */

add_filter( 'image_send_to_editor', 'b5f_image_to_editor', 10, 8 ); 
function b5f_image_to_editor( $html, $id, $caption, $title, $align, $url, $size, $alt )
{
    // Manipulate the HTML
    return $html;
}

在简单测试中传递的值:

[html]      => <a href="http://example.dev/wp-content/uploads/image.png"><img src="http://example.dev/wp-content/uploads/image.png" alt="alternate text" width="128" height="128" class="alignnone size-full wp-image-176" /></a>
[id]        => 176
[caption]   => 
[title]     => 
[align]     => none
[url]       => http://example.dev/wp-content/uploads/image.png
[size]      => full
[alt]       => alternate text
于 2013-10-14T00:29:50.950 回答