I'm using image_send_to_editor to filter the output the images that are inserted to my posts. The filter works perfect except for when I'm updating/saving the post. A new paragraph with a line break inside will appear under the inserted picture on save. So if I save the post 5 times, 5 paragraph tags will appear.. Have a look at the output is after e few saves:

<div class="col-md-6">
<figure class="alignnone"><img src="http://www.travelingthegoodvibes.dev/content/uploads/2015/01/IMG_4761-682x1024.jpg" alt="TEST">
        <p class="image-description"><span>TEST: </span>test</p>

annoying, huh? This is how the filter looks:

function title_caption_image($html, $id, $caption, $title, $align, $url, $size, $alt) {
    $src  = wp_get_attachment_image_src( $id, $size, false );
    $html5 = "<figure class='align$align'>";
    $html5 .= "<img src='$src[0]' alt='$alt' />";
    if ( $caption && $alt ) {
        $html5 .= "<figcaption><p class='image-description'><span>$alt: </span>$caption</p></figcaption>";
    $html5 .= "</figure>";
    return $html5;

Any suggestion on what the source of the problem is? And how I can fix it? Thanks


Disabling wpautop does not solve the problem.

And I found this:

But even if you’d use wp_get_attachment_image_src() or get_image_tag() to generate the image the WYSIWYG editor does not accept the figure element and will append a new line in front and after it everytime you save the article. Source

I tried without the html5 tags, and it still generates empty paragraph tags.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.